Copied to clipboard

Flag this post as spam?

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


  • Carl 65 posts 87 karma points MVP
    Mar 05, 2010 @ 18:48
    Carl
    0

    Problem with conditionally setting a variable

    Hi All

    I'm getting erros when I try to conditionally assign a variable in an XSLT. Essentially what I'm trying to do is this.

    If the current level is 2, then pick assign current node to a variable, if the level is 3, assign the parent node to a variable.The snippet I'm having problems with is below

    <xsl:variable name="rootNode">
        <xsl:choose>
            <xsl:when test="@level = 3">
                <xsl:value-of select="$currentPage/ancestor-or-self::node [@level=2]"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$currentPage"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    And the error I get is To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function.

    The thing I don't get is that if I take out the choose statement, and assign the variable to a single value like below, it works.

    <xsl:variable name="rootNode" select="$currentPage"/>

    What gives?

  • Tommy Poulsen 514 posts 708 karma points
    Mar 05, 2010 @ 19:43
    Tommy Poulsen
    0

    Hi Carl, I suppose you get the error when you save the xslt, right? I can save your snippet from above without any errors.

    >Tommy

     

  • Tommy Poulsen 514 posts 708 karma points
    Mar 05, 2010 @ 19:45
    Tommy Poulsen
    0

    aah, sorry - my bad - I have to use the var.

  • Tommy Poulsen 514 posts 708 karma points
    Mar 05, 2010 @ 20:02
    Tommy Poulsen
    0

    try adding a new variable select your rootNode

    <xsl:variable name="myRootNode" select="$rootNode"/>

    and then use this instead

    <xsl:value-of select="$myRootNode"/>

    >Tommy

  • dandrayne 1138 posts 2262 karma points
    Mar 05, 2010 @ 20:32
    dandrayne
    1

    If you always just want to select a level2 node, you can use

    <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::node [@level=2]" />

    This will work on level 2, 3, 4 etc, all the way down.

     

  • Carl 65 posts 87 karma points MVP
    Mar 07, 2010 @ 15:36
    Carl
    0

    Hi Dan

     

    Thanks for spotting that - Completely missed the wood for the trees. Spent too long stating at that.

     

    cheers

  • Carl 65 posts 87 karma points MVP
    Mar 07, 2010 @ 15:57
    Carl
    0

    I am curious though why I got the error when using just currentNode rather than specifically picking a node - as in my snippet in my original post. Anyone know why?

    The error I was getting was

     

    System.Xml.Xsl.XslTransformException: To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function.
    at System.Xml.Xsl.Runtime.XsltConvert.EnsureNodeSet(IList`1 listItems)
    at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
    at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results)
    at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

     

  • Tommy Poulsen 514 posts 708 karma points
    Mar 07, 2010 @ 16:19
    Tommy Poulsen
    0

    Apparently the expression is evaluated as a string instead of a node set when not selecting directly as a property on the xsl:variable element. I.e. when you have a <xsl:variable ... select="..."/> it's interpreted correctly, where as <xsl:variable ...><xsl:value-of ...></xsl:variable> forces interpretation as a string.

    >Tommy

Please Sign in or register to post replies

Write your reply to:

Draft