Copied to clipboard

Flag this post as spam?

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


  • Matthew Wilde 3 posts 23 karma points
    Mar 13, 2012 @ 21:57
    Matthew Wilde
    0

    Trying to create a listing of latest news

    Hi

    I'm new to XSLT files - and new to Umbraco. I'm trying to create my first website.

    I want to display a list of "Latest News" articles on my front page. I have created a Macro parameter so that you can enter the number of articles you want to display. If there is more articles than the parameter I want a link saying something like "More news items" which links to the main news section.

    All is going well - but I cannot work out the logic where to put the code to display the main news section link if there are more records. For testing my parameter is set to 2 records, and there are 4 in total. The 2 are displayed but "more records available" is displayed an extra 2 times - one for each record that isn’t displayed - I just want to see it once.

     

    This is my 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:umbraco.contour="urn:umbraco.contour" xmlns:google.maps="urn:google.maps"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets umbraco.contour google.maps ">

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

    <xsl:param name="currentPage"/>
    <xsl:variable name="source" select="/macro/source"/>
    <xsl:variable name="numberOfItems" select="/macro/numberOfItems"/>
      

    <xsl:template match="/">
    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/* [@isDoc and string(umbracoNaviHide) != '1']">
    <xsl:sort select="@createDate" order="descending"/>
      <xsl:if test="position() &lt;= $numberOfItems">      
         <div class="img-indent">
                  <a href="{umbraco.library:NiceUrl(@id)}">
                        <img>
                            <xsl:attribute name="src">
                            <xsl:text>/ImageGen.ashx?image=</xsl:text>
                            <xsl:value-of select="newsThumbnail"/>
                            <xsl:text>&amp;width=98&amp;height=98%&amp;constrain=true</xsl:text>
                            </xsl:attribute>
                        </img>
                    </a>
            </div>
         
                    <a href="{umbraco.library:NiceUrl(@id)}">
                        <xsl:value-of select="@nodeName"/>
                    </a>
                     <br />
                      <small><xsl:value-of select="umbraco.library:LongDate(newsDate)"/></small>
                     <br/>
                    <xsl:value-of select="newsSummary"/>
                    <br /><br /><br /><br /><br/>

      </xsl:if>
      <xsl:if test="position() &gt; $numberOfItems">
          <p>more records available</p>
       </xsl:if>

    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

     

    Any help would be greatly appreciated.

    Thank you very much

  • Dan 1288 posts 3921 karma points c-trib
    Mar 13, 2012 @ 23:57
    Dan
    1

    Hi Matthew,

    It sounds like you want the 'more records available' text to appear when there are more than x number of records in total, right?

    So, I'd remove the bold code in your post and use something like this instead, but put it after the </xsl:for-each> closure (so it's outside of the loop): 

    <xsl:if test="count(umbraco.library:GetXmlNodeById($source)/* [@isDoc and string(umbracoNaviHide) != '1']) &gt; $numberOfItems">
        <p>more records available</p>
    </xsl:if>

    Hope this does the trick for you...

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 14, 2012 @ 00:01
    Chriztian Steinmeier
    1

    Hi Matthew,

    The "more records available" is displayed (of course) because it's inside the loop - what you want to do is ask for the actual count and not for every position that's over the maximum. You do something like this:

    <!-- Grab the records to show -->
    <xsl:variable name="records" select="umbraco.library:GetXmlNodeById($source)/*[@isDoc][not(umbracoNaviHide = 1)]" />
    
    <xsl:for-each select="$records">
        <xsl:if test="position() &lt;= $numberOfItems">
            <!-- ... -->
        </xsl:if>
    </xsl:for-each>
    
    <xsl:if test="count($records) &gt; $numberOfItems">
        <p>more records available</p>
    </xsl:if>
    

    /Chriztian

  • Matthew Wilde 3 posts 23 karma points
    Mar 14, 2012 @ 10:08
    Matthew Wilde
    0

    Thank you both. That worked perfectly. I spend hours trying to sort that, but knew it must be something simple, as I could have done it in other languages.

    Can either of you recommend a good book, or website as a place to start learning this XSL language please? 

     

    Cheers again for the help.

    Matthew

  • Matthew Wilde 3 posts 23 karma points
    Mar 14, 2012 @ 10:09
    Matthew Wilde
    0

    Am i not able to mark both as the solution? They both worked.

  • Dan 1288 posts 3921 karma points c-trib
    Mar 14, 2012 @ 10:52
    Dan
    2

    Great, glad that worked for you Matthew.

    In terms of learning XSLT, personally I've just done it through reading online tutorials and I had a lot of help here from kind folks like Chriztian and Lee Kelleher.  Chriztian actually has a great site with some useful XSLT resources on it: http://pimpmyxslt.com/ so that's definitely worth checking out (particularly the XPath Axes Visualizer).

    One thing to bear in mind though, if you're in Umbraco for the long-term...  In version 5 of Umbraco, XSLT is essentially deprecated.  The old versions (right the way up to 4.7.1.1 today) use an xml cache to store your website data, so XSLT is the obvious language of choice to manipulate this content into HTML.  In Umbraco 5, the cacheing isn't based on xml so there's no clear route to be able to manipulate your content though XSLT.  Instead, Razor is the language of choice.  So it may be prudent to invest your time learning razor rather than XSLT, particularly since you can actually use Razor in 4.7 too.  I'm not saying do it, just pointing it out as an alternative :)

    Personally I haven't got very far into Razor as my early experiences of it were that there were so many idiosyncrasies with the way Razor and Umbraco integrate (quite buggy and prone to major changes in the very eary implementations at least) that it became unproductive from a development point of view and tainted what should have been a smooth experience.  So personally, I'm going to wait until a much later version of Umbraco 5 before I delve more deeply into Razor, by which time the implementation has hopefully matured into the elegant view engine it promises to be.  Until then I'm still very happy with XSLT.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 14, 2012 @ 11:04
    Chriztian Steinmeier
    0

    Hi Matthew,

    No worries - it worked and Dan's was the first so go with that. No one here is in it "for the money" so to speak :-)

    Regarding learning XSLT - the best thing I ever did was to print a bunch of XSLT files on real(!) paper and reading them everyday on the bus :-)

    I'd suggest you take a look at my "XSLT Helpers" project on GitHub (not linking to it - you can find it, I'm sure :-) and look through those just to learn how XSLT code "looks", because it's actually very different from C# and other "normal" languages.

    @Dan: Thanks for mentioning (and pimpin') my stuff :-)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft