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.
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']) > $numberOfItems"> <p>more records available</p> </xsl:if>
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() <= $numberOfItems">
<!-- ... -->
</xsl:if>
</xsl:for-each>
<xsl:if test="count($records) > $numberOfItems">
<p>more records available</p>
</xsl:if>
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?
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.
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 :-)
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 " "> ]>
<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() <= $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>&width=98&height=98%&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() > $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
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):
Hope this does the trick for you...
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:
/Chriztian
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
Am i not able to mark both as the solution? They both worked.
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.
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
is working on a reply...