Copied to clipboard

Flag this post as spam?

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


  • Geoff Stokes 19 posts 38 karma points
    Sep 02, 2011 @ 11:02
    Geoff Stokes
    0

    First subnode

    I have an xslt macro which loops through a set of items. I need to get a piece of information from the first of each set.

    How do I use an attribute from the first item?

  • Frederik T 239 posts 370 karma points
    Sep 02, 2011 @ 11:09
    Frederik T
    3

    Great! Finally something i think i can help with.

    What you need to use is "position()", here is a stupid example but hopefully it will help you:

    the start of your loop (dont use my select statement, it might be different in you case its just an example)
    <
    xsl:for-each select="$currentPage/ancestor-or-self::* [@isDoc and @level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
    <xsl:choose>
    What this does is, when the loop hits the first item
        <xsl:when test="position() = 1">
    Do something with the first item
    <xsl:when>
    Now this is when it loops through the rest
    <xsl:when test="position() >= 2">
    Do something with the rest
    <xsl:when>
    <xsl:choose>
    <xsl:for-each>
  • Frederik T 239 posts 370 karma points
    Sep 02, 2011 @ 11:13
    Frederik T
    0

    Ok, going to double post because the forum changes my text each time i edit,

    This is wrong:

    <xsl:whentest="position() >= 2">

    it should be

    <xsl:when test="position() &gt;= 2">
  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Sep 02, 2011 @ 12:35
    Chriztian Steinmeier
    1

    Hi Geoff,

    Taking an attribute from the first of somethings in a set is done by appending a [1] predicate before the attribute selector, e.g.:

    <xsl:value-of select="$siteRoot/NewsList/NewsItem[1]/@nodeName" />

    If you need to go through them all anyway, use Frederik's suggestion and test whether you're at the first item - same example:

    <xsl:for-each select="$siteRoot/NewsList/NewsItem">
       <xsl:if test="position() = 1">
          <xsl:value-of select="@nodeName" />
       </xsl:if>
       ...
    </xsl:for-each>

    /Chriztian

    PS: Actually, Frederik - you *are* allowed to write >= inside a string value. The only characters you MUST escape at all times are the ampersand ( & ) and the less-than ( < ) characters. That means that if you don't like to write this:

    <xsl:if test="somePropertyValue &lt; 500">

    You can rewrite it like this, and it'll be valid:

    <xsl:if test="500 > somePropertyValue">

    - but my head doesn't like that way of comparing at all :-)

     

  • Geoff Stokes 19 posts 38 karma points
    Sep 05, 2011 @ 05:41
    Geoff Stokes
    0

    Why does using

    <href="{umbraco.library:NiceUrl($cell/event[1]/@id)}">

    just give me a System.OverflowException error? I don't understand.

    How can I check if an element exists in the array?

  • Geoff Stokes 19 posts 38 karma points
    Sep 05, 2011 @ 05:58
    Geoff Stokes
    0

    Oh okay, I figured it out, there was no checking to verify there was a value present, so it was failing on those grounds.

Please Sign in or register to post replies

Write your reply to:

Draft