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 June 22nd, 2007, 09:23 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with variable data-type

Hi,

I'm new to xml and I'm having problems with variables:
I have one variable defined before a for-each loop:
<xsl:variable name="nameList"><xsl:value-of select="output-flow/@class.name"/></xsl:variable>
which is compared with the value of the attribute "name" of the element "class" inside the loop:

<xsl:for-each select="package/class">
   <xsl:if test="$nameList=@name">
       (here comes some code)
   </xsl:if>
</xsl:for-each>

But the test is not working as I expected, unless I write:
    <xsl:if test="string($nameList)=@name">

Has anybody any idea??
 
Old June 22nd, 2007, 09:33 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Are you using XSLT 1.0 or 2.0?

I can't think immediately of any reason why the comparison should work with string() and not without it.

But you shouldn't get into the habit of writing

<xsl:variable name="nameList"><xsl:value-of select="output-flow/@class.name"/></xsl:variable>

It's very commonly done, and it's crazy. Write:

<xsl:variable name="nameList" select="output-flow/@class.name"/>

Otherwise you're creating a new temporary document node, and copying the attribute to a new text node, when all you want is a reference to the original attribute.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 22nd, 2007, 09:56 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mhkay,

I'm using XSLT 1.0 and by the moment we can't move to XSLT 2.0.

Thank for your recommendation, from now on I'll use it to define the variables. But the fact is that I obtain the same result in the comparision with both of them.
And now we're starting to be afraid of having done more comparisons without writing "string" and which could need it.

Thanks for your attention
 
Old June 22nd, 2007, 10:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There is one case where string(@attr)=x gives a different result from @attr=x, namely when @attr does not exist and x is "". In this situation @attr="" gives false, while string(@attr)="" gives true.

However, this doesn't apply your situation where you defined the variable as a result tree fragment, since that can never be empty (assuming the product implements the spec correctly - which one can't always rely on)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 22nd, 2007, 10:39 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi again,

thank you very much again for your time. I'll continue looking for the solution and promise to tell you when found!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
declare a ASP variable with a TYPE crmpicco Classic ASP Basics 1 August 17th, 2005 05:52 PM
Interesting Data Type Problem dlandolin SQL Server 2000 3 February 23rd, 2005 06:23 PM
Data type mismatch problem dominic_huang Classic ASP Databases 3 November 15th, 2004 03:12 AM
Getting Problem in DATE/TIME Data type in MS Acces selam70 Classic ASP Databases 2 September 4th, 2004 02:31 PM
Data Type error but it is the correct type Mitch SQL Server 2000 2 March 19th, 2004 11:31 AM





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