Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bent Holz 94 posts 257 karma points
    Oct 20, 2011 @ 14:41
    Bent Holz
    0

    News list pages using xslt

    I'm trying to make a newslist in umbraco 4.7.1 using xslt, where the list is limited to a defined number of newsitems...

    I have the following pagesetup:

    News - News 1
    - News 2
    - News 3
    - etc.

     

    I have managed to make the list, sort it according to date, limit the list to 4 items, but then i'm stuck...

    How do I go about adding a "previous / next " function?...

    My code:

    <xsl:template match="/">
            <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">

                    <xsl:sort select="umbraco.library:FormatDateTime(nyhedDato, 'dd MMMM, yyyy')" order="descending" />
                    <xsl:if test="position() &lt;= 4">
                    <div>
                      <p><small><xsl:value-of select="umbraco.library:FormatDateTime(nyhedDato, 'dd MMMM, yyyy')"/></small></p>
                      <H2><xsl:value-of select="newsAreaOverskrift"/></H2>
                      <p><xsl:value-of select="nyhedBeskrivelse"/></p><br />
                      </div>
                    </xsl:if>
            </xsl:for-each>
    </xsl:template>

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 20, 2011 @ 14:52
    Fuji Kusaka
    0

    Hi Crawn,

    I havent used v4.7.1 but anyway i think that this should get you working.

    First instead you could change your Sorting by date to something like

    <xsl:variable name="newsitems" select="4"/>

     <xsl:sort select="current()/nyhedDatoorder="descending"/>
     <xsl:if test="position() &lt; = newsitems">  
    .....
               </xsl:if> 

    There is a nice tutorial on paging http://www.nibble.be/?p=11 . 

    //Fuji


  • Bent Holz 94 posts 257 karma points
    Oct 21, 2011 @ 08:22
    Bent Holz
    0

    Hi Fuji...

    Tried your example:

    <xsl:template match="/">
    <xsl:variable name="newsitems" select="4"/>
    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
    <xsl:sort select="current()/nyhedDato" order="descending"/>
    <xsl:if test="position() &lt;= newsitems">
    ...
    </xsl:if>
    </xsl:for-each>
    </xsl:template>

    ...and nothing came out. I must be doing something wrong

    I already looked at the nibble tutorial but couldnt get it to work ( yes i changed all " etc. :) )

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 21, 2011 @ 08:29
    Fuji Kusaka
    0

    Hi there,

    You are not getting any output? I noticed in the code you posted you included the "newsitems" variable in your template.

    Can you place it outside your template

    <xsl:param name="currentPage"/> 
    <xsl:variable name="newsitems" select="4"/>    
    <xsl:template match="/">
    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
    <xsl:sort select="current()/nyhedDato" order="descending"/>
    <xsl:if test="position() &lt;= newsitems">
    ...
    </xsl:if>
    </xsl:for-each>
    </xsl:template>

    Can you post your complete code to better understand and help you with your paging?

    //fuji

  • Bent Holz 94 posts 257 karma points
    Oct 21, 2011 @ 10:11
    Bent Holz
    0

    Hi Fuji

    And thx again for taking your time to help me...

    Complete code:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxml="urn:schemas-microsoft-com:xslt" 
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets PS.XSLTsearch ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/> 
    <xsl:variable name="newsitems" select="4"/>    
    <xsl:template match="/">
      <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
          <xsl:sort select="current()/nyhedDato" order="descending"/>
          <xsl:if test="position() &lt;= newsitems">
            Output...
          </xsl:if>
       </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>  

    //Crawn

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 21, 2011 @ 11:36
    Fuji Kusaka
    0

    Hi Crawn, 

    Can you try something like this, am not sure how you are accessing your folder containing your newsitems. So am just try get it by using the GetXmlNodeById.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxml="urn:schemas-microsoft-com:xslt" 
      xmlns:umbraco.library="urn:umbraco.library"
      xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:umbraco.contour="urn:umbraco.contour" xmlns:ucomponents.nodes="urn:ucomponents.nodes" xmlns:ucomponents.dates="urn:ucomponents.dates" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets umbraco.contour ucomponents.nodes">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>

    <!-- Don't change this, but add a 'contentPicker' element to -->
    <!-- your macro with an alias named 'source' -->
    <xsl:variable name="source" select="1200"/> <!-- enter parent NodeId -->
    <xsl:variable name="newsitems" select="4"/>
    <xsl:variable name="numberOfItems" select="count(umbraco.library:GetXmlNodeById($source)/* [@isDoc and string(umbracoNaviHide) != '1'])"/>
    <xsl:variable name="jump" select="$itemsToDisplay * $pageNumber" />

        
    <!-- Paging -->
    <xsl:variable name="pageNumber">
      <xsl:choose>
        <xsl:when test="umbraco.library:RequestQueryString('page') = ''">
          <xsl:value-of select="1"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>    
        
    <xsl:template match="/">


     <xsl:variable name="sourceXml" select="umbraco.library:GetXmlNodeById($source)/*[@isDoc][not(umbracoNaviHide = 1)]" />  
         
     <xsl:for-each select="$sourceXml">
               <xsl:sort select="current()/@nyhedDato" order="descending"/>
                <xsl:if test="position() &lt;= $jump and position() &gt;= ($jump - $newsitems)">
                      Output ...
                   </xsl:if> 
          </xsl:for-each> 
    <!-- Your paging -->
    <ul>    
          <xsl:if test="$pageNumber &gt; 1">
          <li>
              <href="?page={$pageNumber -1}#newsitems">previous </a>
         </li>
        </xsl:if>

              <xsl:call-template name="for.loop">
                 <xsl:with-param name="i">1</xsl:with-param>
                  <xsl:with-param name="count" select="ceiling($numberOfItems div $itemsToDisplay)"></xsl:with-param>
            </xsl:call-template>  

        <xsl:if test="(($pageNumber) * $itemsToDisplay) &lt; ($numberOfItems)">
             <li>
                <href="?page={$pageNumber+1}#newsitems">next</a>
           </li>
    </xsl:if>     
    </ul>  
    </xsl:template>

     <xsl:template name="for.loop">     
                    <xsl:param name="i"/>
                    <xsl:param name="count"/>

                    <xsl:if test="$i &lt;=$count"                        
                             <xsl:if test="$pageNumber!= $i">
                             
                                     
    <li>
                                    
                                      <href="?page={$i}#
    newsitems"><xsl:value-of select="$i" />&nbsp;</a>
                                 </li>
                            </xsl:if>
                            
    <xsl:if test="$pageNumber = $i">
                              <li>
                                    <xsl:value-of select="$i"/>&nbsp;
                              </li>
                            </xsl:if                        

    <
    xsl:call-template name="for.loop">
                                   <xsl:with-param name="i" select="$i + 1" />
                                    <xsl:with-param name="count" select="$count">
                                    </xsl:with-param>
                            </xsl:call-template                
    </xsl:if>
      
    </xsl:template>
    </xsl:stylesheet>

    Hope this will solve your problem

    //Fuji

  • Bent Holz 94 posts 257 karma points
    Oct 24, 2011 @ 09:09
    Bent Holz
    0

    Hi Fuji...

    I made the Xslt from the std. "List subpages from a changeable source", setup "source" in the macro, and added the macro to the template selecting the parent (id 1107) of my news items.

     

    If i insert all of your code and change the parent node to "1107" i get the following error:

    Error occured

    System.Xml.Xsl.XslLoadException: The variable or parameter 'itemsToDisplay' is either not defined or it is out of scope.

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 24, 2011 @ 09:18
    Fuji Kusaka
    0

    Hey Crawn Sorry my mistake!!

    Change this part to 

     

    <xsl:variable name="itemsToDisplay" select="4"/>

     

    and this as well

     <xsl:if test="position() = ($jump - $itemsToDisplay)">
                      Output ...
    <xsl:if> 
  • Bent Holz 94 posts 257 karma points
    Oct 24, 2011 @ 13:58
    Bent Holz
    0

    Hi Fuji...

    Getting close now...

    With the above code, the paging is working correct, from what i can see... If i have say 6 newsitems and set "newsitems" to "1" paging generates "1 2 3 4 5 6" and "next"... But there is something about the "Output ..."

    If newsitems = 4, paging will correctly be " 1 2 next" but the output shold be 4 x "Output ..." on page 1 and on page 2 "Output ..." only shows up once (should be twice).

    Do i need another "<xsl:for-each..." around "Output..." in the code below or is it something completely else, that im not getting?

    <xsl:for-each select="$sourceXml">
      <xsl:sort select="current()/@nyhedDato" order="descending"/>
        <xsl:if test="position() = ($jump - $itemsToDisplay)">
          Output ...
        </xsl:if
    </xsl:for-each

  • Bent Holz 94 posts 257 karma points
    Oct 24, 2011 @ 13:58
    Bent Holz
    0

    Hi Fuji...

    Getting close now...

    With the above code, the paging is working correct, from what i can see... If i have say 6 newsitems and set "newsitems" to "1" paging generates "1 2 3 4 5 6" and "next"... But there is something about the "Output ..."

    If newsitems = 4, paging will correctly be " 1 2 next" but the output shold be 4 x "Output ..." on page 1 and on page 2 "Output ..." only shows up once (should be twice).

    Do i need another "<xsl:for-each..." around "Output..." in the code below or is it something completely else, that im not getting?

    <xsl:for-each select="$sourceXml">
      <xsl:sort select="current()/@nyhedDato" order="descending"/>
        <xsl:if test="position() = ($jump - $itemsToDisplay)">
          Output ...
        </xsl:if
    </xsl:for-each

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 24, 2011 @ 14:02
    Fuji Kusaka
    0

    Can you do try something like

    <xsl:if test="position() &lt;= $jump and position() &gt;= ($jump - $itemsToDisplay)">
                      Output ...
                   </xsl:if> 

     

  • Bent Holz 94 posts 257 karma points
    Oct 24, 2011 @ 14:37
    Bent Holz
    0

    Well it brought us closer and with a little adjustment (removal of "=") so that the last item on current page doesn't show up as first item on next page:

        <xsl:if test="position() &lt;= $jump and position() &gt; ($jump - $itemsToDisplay)">

    IT WORKS PERFECTLY!... and event better from your code and explaining I get most of what is happening :)

    Thumbs up! 

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 24, 2011 @ 14:39
    Fuji Kusaka
    0

    Great if you got it working !!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies