Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 17th, 2004, 07:14 AM
Registered User
 
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL:CHOOSE & XPATH

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?


 
Old November 17th, 2004, 07:29 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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:
Code:
<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)
 
Old November 17th, 2004, 07:44 AM
Registered User
 
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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]" />

 
Old November 17th, 2004, 08:36 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL Param with XPath expressions V4mp1r3 XSLT 7 June 1st, 2007 11:47 AM
String xsl:param as XPath expression Vx XSLT 4 September 5th, 2006 10:18 AM
chapter4 xsl and xpath problem clinfix BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 August 8th, 2006 02:21 PM
xsl:choose djmarquette XSLT 5 March 29th, 2005 11:44 AM
<xsl:choose> and <xsl:otherwise> problem djmarquette XSLT 4 January 21st, 2005 01:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.