Link to home
Start Free TrialLog in
Avatar of net_susan
net_susan

asked on

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?
      
            
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of net_susan
net_susan

ASKER

What does this do:

*[name()
[] 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
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>
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
Darn, this isn't working at all. Thought it was. Closed it too soon.
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>
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.
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
Okay, thank you. It is working. I appreciate you coming back even after I awarded points. I'll probably post another related question later.