XSL Variables

I'm trying to create a variable in my XSL stylesheet.
This line works:
<xsl:variable name="Rows" select="/dsQueryResponse/Some_List/Rows/Row[normalize-space(@Page) = 'Home']"/>

What I want to do is something like this:
<xsl:variable name="Susan">Some_List</xsl:variable>
 <xsl:variable name="Rows" select="/dsQueryResponse/@Susan/Rows/Row[normalize-space(@Page) = 'Home']"/>

What am I doing wrong?
      
            
LVL 1
net_susanAsked:
Who is Participating?
 
Geert BormansInformation ArchitectCommented:
<xsl:variable name="Rows" select="/dsQueryResponse/*[name() = $Susan]/Rows/Row[normalize-space(@Page) = 'Home']"/>
0
 
Geert BormansInformation ArchitectCommented:
you can't have dynamic XPath
so you need to do a string compare, matching the name

you access variables with a $, not with a @

cheers

Geert
0
 
net_susanAuthor Commented:
What does this do:

*[name()
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Geert BormansInformation ArchitectCommented:
[] is the indication of a "predicate", a special condition on eg. an element
* is a wild-card for element nodes
name() is the XPath function that returns the name of the node

*[name() = $Susan]
is the element that has a name equal to the content of variable $Susan
0
 
net_susanAuthor Commented:
You rock.

If you would, I'd like to know if these two options are kosher (they both seem to work), but either way you get the points because you answered my question, and it works!.

Option One:
<Xsl>
<xsl:variable name="Susan">Some_List</xsl:variable>
</Xsl>
<xsllink>http://somepage.com/SomeTemplate.xsl</xsllink>

Option Two:
<xsllink>http://somepage.com/SomeTemplateWithVar.xsl</xsllink>
<xsllink>http://somepage.com/SomeTemplate.xsl</xsllink>

Contents of SomeTemplateWithVar.xsl:

<xsl:stylesheet xmlns:xs="deleted for space">
<xsl:variable name="Susan">Some_List</xsl:variable>
</xsl:stylesheet>
0
 
Geert BormansInformation ArchitectCommented:
both options are OK
you might consider to make this a parameter instead of a variable
<xsl:param name="Susan">Some_List</xsl:param>

this would allow you to pass the value in from outside
In that case I prefer to keep the parameter in the main XSL

cheers

Geert
0
 
net_susanAuthor Commented:
Darn, this isn't working at all. Thought it was. Closed it too soon.
0
 
Geert BormansInformation ArchitectCommented:
You must have invented your own syntax for including xsl files
(I imagined that you were processing this first in order to create real XSLT)

Here is the correct syntax
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:variable name="Susan">Some_List</xsl:variable>
    <xsl:include href="http://somepage.com/SomeTemplate.xsl" />
    ...
</xsl:stylesheet>
0
 
net_susanAuthor Commented:
I don't know why that's not working for me. I did want to call the vars from outside the stylesheet, but I guess I wasn't clear about that. That was my whole point of doing it.

Changing this <xsl:variable name="Susan">Some_List</xsl:variable> to this
<xsl:param name="Susan">Some_List</xsl:param> completely breaks it, even inside the sheet.  :(

Still, you have gotton me closer I'm sure.
0
 
Geert BormansInformation ArchitectCommented:
That means that the xsl:variable is at a location where param is not allowed
If you want to pass in a parameter,
it should be a top level element
As in my example, move the param to right under the stylesheet element
0
 
net_susanAuthor Commented:
Okay, thank you. It is working. I appreciate you coming back even after I awarded points. I'll probably post another related question later.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.