Copied to clipboard

Flag this post as spam?

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


  • Carl Fraser 10 posts 40 karma points
    Mar 06, 2013 @ 18:37
    Carl Fraser
    0

    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 "&#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: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>

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 06, 2013 @ 21:38
    Jan Skovgaard
    100

    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

  • Carl Fraser 10 posts 40 karma points
    Mar 07, 2013 @ 08:19
    Carl Fraser
    0

    Hi Jan

    Many thanks for that - that's great

    Much appreciated

    Best regards

    Carl

     

Please Sign in or register to post replies

Write your reply to:

Draft