Subject: XSL:CHOOSE & XPATH
Posted By: falmouth Post Date: 11/17/2004 6:14:49 AM
Depending on an already defined variable value, I'd like to set the value of a variable to an XPath. For example:

<xsl:variable name="xpath">
<xsl:choose>
<xsl:when test="$parent != 0">$structure/structure/*/*[@id = name(current())]</xsl:when>
<xsl:otherwise><xsl:value-of select="$structure/structure/*[@id = name(current())]</xsl:otherwise>
</xsl:choose>
</xsl:variable>


And then reference the path along the lines of this...

<xsl:variable name="type"><xsl:value-of select="name($xpath)" /></xsl:variable>

...or...

<xsl:variable name="type"><xsl:value-of select="$xpath/@attribute" /></xsl:variable>

Of course, thats not going to work as I need to define the XPath within the xpath variable using the select attribute. Any suggestions?


Reply By: joefawcett Reply Date: 11/17/2004 6:29:16 AM
Without extensions you can't use dynamic paths. If one of the expressions will always return a null node set then you can use the union operator:
<xsl:variable name=xpath" select="(expression1 | expression2)[1]"/>

Or if one has priority if both are not null then put that first.



--

Joe (Microsoft MVP - XML)
Reply By: falmouth Reply Date: 11/17/2004 6:44:35 AM
Thanks for that. It seems to work like a dream. So, the expression that I place first will have priority over the other?

I am now using...

<xsl:variable name="xpath" select="($structure/structure/group/*[@id = name(current())]|$structure/structure/*[@id = name(current())])[1]" />

Reply By: joefawcett Reply Date: 11/17/2004 7:36:13 AM
Well you are creating a union of both selects. If one is null then it won't be included. You are then picking the first node in the set. This assumes each XPath returns at most one node at its root.

--

Joe (Microsoft MVP - XML)

Go to topic 22118

Return to index page 713
Return to index page 712
Return to index page 711
Return to index page 710
Return to index page 709
Return to index page 708
Return to index page 707
Return to index page 706
Return to index page 705
Return to index page 704