Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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 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>
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
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.

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

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