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
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)
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.
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?
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
aah, sorry - my bad - I have to use the var.
try adding a new variable select your rootNode
and then use this instead
>Tommy
If you always just want to select a level2 node, you can use
This will work on level 2, 3, 4 etc, all the way down.
Hi Dan
Thanks for spotting that - Completely missed the wood for the trees. Spent too long stating at that.
cheers
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)
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
is working on a reply...