Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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..
Reply With Quote
  #2 (permalink)  
Old July 2nd, 2009, 04:02 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
tieme (July 2nd, 2009)
  #3 (permalink)  
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
Reply With Quote
  #4 (permalink)  
Old July 2nd, 2009, 04:43 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:57 AM.


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