 |
| 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
|
|
|
|

November 5th, 2005, 06:37 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Using XSLT arguments for in XML paths
Currently I am trying to use the XsltArgumentList in XML transformation done from code-behind in an ASP.NET application. However, I have some problems using the arguments in XML paths. In the following code I would like to replace someone with the user argument, as used later in an attribute...
Code:
<xsl:template match="users">
<xsl:apply-templates select="user[@name='someone']" />
</xsl:template>
<xsl:template match="user[@name='someone']">
<input type="text" value="{$user}" />
</xsl:template>
I have tried using the same notation but without succes. Is it possible and so, how?
Thanks, Jacob.
|
|

November 5th, 2005, 06:49 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
XSLT 1.0 doesn't allow a variable to be used in a match pattern. This restriction is removed in XSLT 2.0. (It has also been removed in some products which in other respects conform to the XSLT 1.0 spec.)
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

November 5th, 2005, 06:55 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Alright, thanks for the speedy reply.
I guess I have to try it is possible in some other way, e.g. using if statement or like. I am trying to make a style sheet, which will display only one profile from a number of users in the XML file. Though the correct way would be to select and not eliminate.
Thanks, Jacob.
|
|

November 5th, 2005, 07:01 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Damn, it doesn't seem to work either. I guess I am out of luck. Isn't there another way to make processing logic based on an argument? Tried this
Code:
<xsl:if test="@name='{$user}'">
... but not working the way I want.
Jacob.
|
|

November 5th, 2005, 08:19 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You only want curly braces around the outside of an XPath expression that's embedded in an attribute whose value would otherwise be literal text (these are called attribute value templates). In the middle of an XPath expression, just write:
<xsl:if test="@name=$user">
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

November 7th, 2005, 06:16 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Coooool Michael Kay... That was great.
Thought I had tried that combination (without quotes and curly braces). Thanks for the clarification on the use of curly braces; so the right-hand side expression in an if is an XPath and the variable is allowed, and in the match expression it is not. I guess the match expression is also an XPath so it must clearly be a deliberate restriction not to allow variables, right?
Thanks again, Jacob.
|
|

November 7th, 2005, 10:03 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
XSLT match patterns are actually a subset of XPath expressions (2+2 is a valid XPath expression but not a valid pattern). Yes, it was a deliberate decision to disallow variables. The reason (misguided with hindsight) was to prevent circular definitions such as
<xsl:variable name="x">
<xsl:apply-templates select="//a"/>
</xsl:variable>
<xsl:template match="a[$x = 3]">
Looping!
</xsl:template>
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

November 7th, 2005, 11:04 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ahhh, I see. Thanks for your help, Jacob.
|
|
 |