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 July 2nd, 2009, 03:02 AM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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 03:06 AM..
 
Old July 2nd, 2009, 04:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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:
tieme (July 2nd, 2009)
 
Old July 2nd, 2009, 04:35 AM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
 
Old July 2nd, 2009, 04:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to assign one variable value to another xsl variable in xsl Anil_Base XSLT 3 March 24th, 2009 01:17 PM
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
xsl:variable holding name of an xsl:param perissos XSLT 0 December 5th, 2006 07:09 AM
XSL Variable and/or Parameter Question ripple XSLT 9 September 1st, 2006 12:16 PM
The difference between xsl:variable and xsl:param NEO1976 XML 2 July 24th, 2006 06:05 AM





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