|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

July 2nd, 2009, 04:02 AM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
xsl:variable question
I am trying to use xsl:variable to clean up my code a bit, but I can't quite get it to work how I want it to.
The code is pretty ugly due to huge xPath expressions (main reason I want to use variables) so I'll just show you the code with the long paths changed to short ones.
I am looking for a more concise way to get the functionality of this code:
Code:
<xsl:if text="count(foo2/bar2[foo/bar = 'CNTY1' or foo/bar = 'CNTY2']) = 0">
<!-- code here -->
</xsl:if>
Code:
<xsl:variable name="countyPath" select="'foo/bar'" />
<xsl:variable name="allCountyPath" select="$countyPath = 'CNTY1' or $countyPath = 'CNTY2'/>
.
.
.
<xsl:if test="count(foo2/bar2[allCountyPath]) = 0">
<!-- code here -->
</xsl:if>
I've tried removing the inner single quotes from the select attribute on the countyPath variable but it doesn't seem to help. Even though there are counties there, it seems to return 0.
Any ideas on where to go from here?
Edit:Sorry for the generic title, was going to try to make it more descriptive but I can't seem to edit it.
Thanks!
John
Last edited by tieme : July 2nd, 2009 at 04:06 AM.
|

July 2nd, 2009, 05:02 AM
|
 |
Wrox Author
Points: 12,642, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 Posts
|
|
First get rid of the inner quotes.
A variable holds a value (in this case, a set of nodes), it doesn't hold a path expression. When you write <xsl:variable name="x" select="foo/bar"/>, it evaluates foo/bar in the current context to return a set of nodes. You seem to be under the incorrect impression that the value of the variable will be a path expression that is evaluated in the context where it is used.
In XSLT 2.0, you can write f/b[foo/bar = ('C1', 'C2')]. In XSLT 1.0, you can write f/b[foo/bar[. = 'C1' or . = 'C2']].
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|
The Following User Says Thank You to mhkay For This Useful Post:
|
|

July 2nd, 2009, 05:35 AM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Michael,
Thanks for the help! It is exactly the information I was looking for. In my defense, the W3Schools page does not indicate that xsl:variable is generally used to hold a set of nodes, and their only example uses it to hold a string (hence the inner quotes). I think I need to purchase your book...
John
|

July 2nd, 2009, 05:43 AM
|
 |
Wrox Author
Points: 12,642, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 Posts
|
|
I find w3schools a really good place when I need to be reminded of half-forgotten syntax details, but a pretty poor place for learning new concepts. Yes - get yourself a good book!
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |