Copied to clipboard

Flag this post as spam?

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


  • TaoistTotty 246 posts 314 karma points
    Aug 01, 2010 @ 14:41
    TaoistTotty
    0

    position() = 1 not working in a template

    Sorry, I know yet another question.

    I am trying to find the first and last item in a list.

    The code below works for the last, but not for the first.

    What have I missed?

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;">
                                <!ENTITY hidden "umbracoNaviHide = 1">
                                <!ENTITY node "*[@isDoc]">
    ]>
    <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"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

      <xsl:variable name="homeMenu" select="/macro/homeItem"/>
      <xsl:if test="$homeMenu !=''">
        <xsl:variable name="homePage" select="umbraco.library:GetXmlNodeById($homeMenu)"/>
        <ul>
          <xsl:apply-templates select="$homePage | $homePage/&node;"/>
        </ul>
      </xsl:if>

    </xsl:template>
    <xsl:template match="&node;">
      <li>
        <xsl:if test="position() = 1">
          <xsl:attribute name="class">
            first
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="position() = last()">
          <xsl:attribute name="class">
            last
          </xsl:attribute>
        </xsl:if>
        <a href="{umbraco.library:NiceUrl(@id)}">
          <xsl:if test="@id = $currentPage/@id">
            <xsl:attribute name="class">
              current
            </xsl:attribute>
          </xsl:if>
          <xsl:value-of select="@nodeName"/>
        </a>
      </li>
    </xsl:template>
        <xsl:template match="&node;[&hidden;]"/>    
    </xsl:stylesheet>

    Many thanks.

    TT

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 01, 2010 @ 14:46
    Sebastiaan Janssen
    0

    Ah, the wonders of XSLT... Calling position() actually returns a string, so this will work:

    <xsl:if test="position() = '1'">
  • TaoistTotty 246 posts 314 karma points
    Aug 01, 2010 @ 15:02
    TaoistTotty
    0

    Sebastiaan

    Thanks for this, this has worked on one menu, but not another.

    I am now trying to look as to why (it is the same code. I am going to try and rename items just in case the two are causing problems with each other).

    Thanks for the solution to the first one.

    TT

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 01, 2010 @ 15:06
    Sebastiaan Janssen
    2

    You could always convert the position into a number and it should work consitently (although, I'd argue that it should do the same for checking it as a string...):

    <xsl:if test="number(position()) = 1">

    It's either that or the position really is not equal to 1.

  • TaoistTotty 246 posts 314 karma points
    Aug 01, 2010 @ 15:13
    TaoistTotty
    1

    Sebastiaan

    Found the problem, I need a better brain.

    I had a problem within the XSLT as I was using it, namely the first page was hidden from navigation, all solved now.

    Thanks for the tip about the position returning a string (one has to ask why).

    Thanks

    TT

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 01, 2010 @ 15:41
    Sebastiaan Janssen
    0

    Yes, that is especially strange (I just learned this myself!) as it used to return a number on another .net project I've worked on before. So I am a bit puzzled as well.

    Glad you got it worked out though!

  • TaoistTotty 246 posts 314 karma points
    Aug 01, 2010 @ 15:52
    TaoistTotty
    0

    I thanks to your help.

    It would be interesting to find out why position returns a string.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 02, 2010 @ 01:58
    Chriztian Steinmeier
    1

    Hi (all),

    I feel I need to chip in and say that position() does not return a string, but you could somehow say, that in XSLT you typically get the type you need without having to worry too much about it (I like that).

    TT's problem here ended up being that position() was in fact never 1 inside the template - otherwise it would have worked without 'stringifying'.

    If you use value-of you typically get a string back, but even then you can easily perform number stuff without explicit casting.

    The only time you really need to cast variables in XSLT is when what you're trying to do could be mis-interpreted (e.g., when you find that adding 5 and 3 gives you 53 instead of 8)

    /Chriztian 

  • TaoistTotty 246 posts 314 karma points
    Aug 02, 2010 @ 09:25
    TaoistTotty
    0

    Hi Chriztian

    Thanks for this.

    Have just checked and it now works with, or without the quotation mark.

    Thanks

    TT

Please Sign in or register to post replies

Write your reply to:

Draft