Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
| 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
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 1st, 2007, 05:22 PM
Registered User
Join Date: Jul 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default variables in predicates

can someone explain to me why these produce two different outcomes
<p>Direct produces the correct result
<p>Variable does not

i thought variables were allowed in predicates

    <xsl:variable name="avariable">somenode</xsl:variable>

    <p>Direct: <xsl:value-of select="count(//*[somenode != 0])" /></p> 
    <p>variable: <xsl:value-of select="count(//*[$avariable != 0])" /></p>
Reply With Quote
  #2 (permalink)  
Old August 1st, 2007, 05:46 PM
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

Yes, variables are allowed in predicates.

Your first expression
 <xsl:value-of select="count(//*[somenode != 0])" /></p>

means "count the number of element nodes in the document that have a child element called "somenode" whose string value, converted to a number, is not equal to zero"

Your second expression
<xsl:value-of select="count(//*[$avariable != 0])" /></p>

means "if the value of $avariable is not equal to zero (which is true, because it is equal to the string "somenode") then count the number of nodes in the document, otherwise return zero"

The two expressions are both valid, but they couldn't have more different meanings.

I think you're making the mistake of thinking that variables represent textual pieces of an expression, like variables in a macro language or in some shell script languages. They don't - they represent values.

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old August 2nd, 2007, 09:59 AM
Registered User
Join Date: Jul 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

with some help of predicates within predicates http://www.ecos.sourceware.org/ml/xs.../msg00522.html

i was able to get done what i needed or it appears so... still have a few more things to test [anothernode=$x and *[name()=$avariable and . != 0]]

i have bought numerous books over the last decade and taught myself
vb6,c#,java,php,c++,sql and a few other languages along with "design theory" as it pertains to oop principles, patterns etc... and would consider myself pretty good in all those except c++ which i'm still learning. However, with this stuff the concepts run so counter to my intuition that it is really frustrating. Not giving up but after dealing with various transformation issues over the last couple of weeks i had to vent a little bit.

Reply With Quote

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
variables daniel.mihalcea Javascript 1 September 9th, 2008 07:06 PM
using variables webgrphx Classic ASP Basics 3 April 11th, 2007 06:46 PM
Passing Variables silve1999 Pro Java 1 June 15th, 2006 12:54 PM
Passing variables acko ASP.NET 1.x and 2.0 Application Design 1 December 23rd, 2003 09:40 AM

All times are GMT -4. The time now is 11:58 PM.

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