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 September 22nd, 2010, 09:26 AM
Registered User
 
Join Date: Sep 2010
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Default <xsl:variable> strange results

Hi,

I'm quite new to xslt and have discovered some strange results when using the <xsl:variable>.
Look at these two variables.
Code:
Number 1:
<xsl:variable name="relatedFile" select="/mother/child/file" />
Number 2:
<xsl:variable name="relatedFile2">
 <xsl:value-of select ="/mother/child/file" />
</xsl:variable>
These two variables, even though instanciated a bit differently should hold exactly the same information right?

Next I want to use the variables to get an attribute of the node. Like this:
Code:
<xsl:value-of select"$relatedFile/@key" />
<xsl:value-of select"$relatedFile2/@key" />
But strangly, only relatedFile gives me the key. RelatedFile2 gives me nothing.
And even stranger, if i compare them like this:
Code:
<xsl:if test="$relatedFile=$relatedFile2"> yes they are equal! </xsl:if>
They are equal!

What is going on here??
Thanks for all your help!
 
Old September 22nd, 2010, 09:32 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The results are vastly different. The xsl:value-of creates a text node and in a variable that way you get a result tree fragment that contains a single text node. The xsl:variable with the select attribute simply selects some node(s) in your input tree.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
daheri (September 22nd, 2010)
 
Old September 22nd, 2010, 09:54 AM
Registered User
 
Join Date: Sep 2010
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks for your reply.

Ok so they are then different. But how could I go about to sort out this thing then?

My XML has either a <file> or a <content>
I need to construct a variable that holds a path to the correct one. And the later i need to use it to get some attributes of the node.

Like this:


Code:
<xsl:variable name="relatedFile" select="/mother/child/file" />
<xsl:variable name="relatedContent" select="/mother/child/content" />

<xsl:variable name="path">
<xsl:choose>
<xsl:when test="$relatedFile !=''"><xsl:value-of select="$relatedFile" /></when>
<xsl:when test="$relatedContent !=''"><xsl:value-of select="$relatedContent" /></when>
</xsl:choose>
</xsl:variable>

<xsl:value-of select"$ultimateContent/@key" />
 
Old September 22nd, 2010, 10:02 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

[code]
<xsl:value-of select="(/mother/child/file | /mother/child/content)[1]/@key"/>
[/code
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
daheri (September 22nd, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Global variable restrictions in the <xsl:key> iceandrews XSLT 2 August 25th, 2008 01:16 PM
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06:36 AM
Performance for <xsl:import> and <xsl:include> vikkiefd XSLT 2 April 16th, 2008 08:06 AM
<xsl:for-each> with a variable select="attribute" BeneathClouds XSLT 1 August 1st, 2005 03:36 AM
<xsl:choose> and <xsl:otherwise> problem djmarquette XSLT 4 January 21st, 2005 01:56 PM





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