Copied to clipboard

Flag this post as spam?

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


  • Alex Burr 77 posts 128 karma points
    May 21, 2010 @ 16:03
    Alex Burr
    0

    Parsing nodes NOT from currentPage

    Hi everyone,

    I'm still new to XSL so please have patience. What I am trying to do is set up my content like this:

    - Home
    - News
    -- News Item 1
    -- News Item 2
    -- News Item 3
    - Event List
    -- Event 1
    -- Event 2

    I am able to easily display the News Item nodes on the News page using the following transform:

    <xsl:template match="/">
    <xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
    <xsl:sort select="data[@alias='createDate']" order="descending" />
    <h3 class="newsTitle"><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a></h3>
    <h4 class="newsDate">Posted <xsl:value-of select="umbraco.library:FormatDateTime(@createDate, 'MMMM d, yyyy')"/></h4>
    <p>
    <xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(data [@alias = 'bodyText']), $MaxNoChars, '...')" />
    [<a href="{umbraco.library:NiceUrl(@id)}">Read more</a>]
    </p>
    </xsl:for-each>
    </xsl:template>

    And something similar for Events. However what I would also like to do is list the News Items and Events on the Home page. I am guessing my XSLT for the Home page will be very similar, except with a different select attribute on the xsl:for-each element. But I don't know what I should use!

    Any help is greatly appreciated. I apologize if this has been addressed already but I wasn't quite sure what to search on for this topic.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 21, 2010 @ 18:21
    Jan Skovgaard
    0

    Hi Alex

    This can be achieved in different ways.

    If you are completely new to XSLT I will recommend that you read http://www.w3schools.com/xsl/default.asp and http://www.w3schools.com/xpath/xpath_functions.asp. Please note that some of the version 2 stuff is not yet usable with .NET.

    I also think you should have a look at umbraco.tv where there are some free samples that could probably get you going. And finally it might be worth reading a bit about axes in XPATH here http://umbraco.org/documentation/books/xslt-basics/xpath-axes-and-their-shortcuts

    And now to the examples of how you can achieve your goal.

    By using a XPATH expression like this ancestor-or-self::node you can go all the way back to the root node of the site and then decide, what type of nodes you want to list on a given page.

    A snippet could look like this:

    <xsl:for-each select="$currentPage/ancestor-or-self::node//node[@nodeTypeAlias = 'NewsItem']">
    <!--- Your code here -->

    </xsl:for-each>

    But please note that using the double forward slashes "//" in the above expression could lower the performance as far as I know.

    Another approach could be to use the extension "GetXmlNodeById", which is a part of the umbraco.library.

    Then you could for instance do something like this:

    <xsl:variable name="newsSource" select="umbraco.library:GetXmlNodeById('1090')" />

    <xsl:for-each select="$newsSource/node">

    <!-- your code here -->

    </xsl:for-each>

    in the above sample 1090 is a fictive id and you off course need to replace it with the id of your news section.

    Of course it's not a good idea to hardcode id's so you should probably make use of a content picker property on your "home" node, so you can select the NewsSource from the content tree and then get the value by writing <xsl:variable name="newsSource select="$currentPage/data [@alias ='yourcontentpickeralias'] />.

    It's possible to do it in even more ways but I think this should be enough to get you started - I hope that it all makes sense, otherwise just ask! :-)

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft