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 5th, 2005, 06:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old November 5th, 2005, 06:49 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old November 5th, 2005, 06:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old November 5th, 2005, 07:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old November 5th, 2005, 08:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old November 7th, 2005, 06:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old November 7th, 2005, 10:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old November 7th, 2005, 11:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ahhh, I see. Thanks for your help, Jacob.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining string paths elements into another XML eXhumed XSLT 2 March 19th, 2008 11:06 AM
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
XSLT for complicated xml to xml transf. required doug@sirvisetti XSLT 3 June 17th, 2005 04:26 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





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