Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1183
  • Last Modified:

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?
      
            
0
net_susan
Asked:
net_susan
  • 6
  • 5
1 Solution
 
Geert BormansCommented:
<xsl:variable name="Rows" select="/dsQueryResponse/*[name() = $Susan]/Rows/Row[normalize-space(@Page) = 'Home']"/>
0
 
Geert BormansCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Geert BormansCommented:
[] 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 BormansCommented:
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 BormansCommented:
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 BormansCommented:
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now