I'm new to Umbraco and XSLT and have what should be a simple query, that's driving me mad!
I need to list all news items, but from anywhere on the site. I have the XSLT macro below which works correctly when on the News page, but I need this to work on the home page, and all other pages too. It's obviously something to do with 'currentPage', but i've been struggling for ages with this.
Since you need to be able to list the news from anywhere on the page and not just the currentpage you need to fetch the id of the node containing your nodes instead of using currentPage.
One way to do this is by using umbraco.library:GetXmlNodeById(yournewsnodeid).
Need to list all news items from top-level
Hi.
I'm new to Umbraco and XSLT and have what should be a simple query, that's driving me mad!
I need to list all news items, but from anywhere on the site. I have the XSLT macro below which works correctly when on the News page, but I need this to work on the home page, and all other pages too. It's obviously something to do with 'currentPage', but i've been struggling for ages with this.
Your help would be gratefully received
Many thanks
Best regards
Carl
XSLT Macro to list news items:-
<?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:twitter.library="urn:twitter.library"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets twitter.library ">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage" select="1157" />
<xsl:variable name="documentTypeAlias" select="string('NewsItem')" />
<xsl:template match="/">
<!-- The fun starts here -->
<xsl:for-each select="$currentPage/* [name() = $documentTypeAlias and string(umbracoNaviHide) != '1']">
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="newsSummary"/>
</a> |
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Hi Carl
Since you need to be able to list the news from anywhere on the page and not just the currentpage you need to fetch the id of the node containing your nodes instead of using currentPage.
One way to do this is by using umbraco.library:GetXmlNodeById(yournewsnodeid).
You can use a variable like this
<xsl:variable name="newsNodeId" select="umbraco.library:GetXmlNodeById(yournewsnodeid) />
This gives you all of the XML from the news node and all it's children.
Instead of using the for-each look you can use apply-templates like below.
<xsl:template match="/">
<xsl:apply-templates select="$newsNodeId/NewsItem[normalize-space()]" />
</xsl:template>
<xsl:template match="NewsItem">
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="newsSummary" />
</a>|
</xsl:template>
Let us know if that works for you.
I will recommend that you read this nice article about match templates by Chriztian Steinmeier as well http://pimpmyxslt.com/articles/match-templates-intro/
Hope this helps.
/Jan
Hi Jan
Many thanks for that - that's great
Much appreciated
Best regards
Carl
is working on a reply...