Connecting Tech Pros Worldwide Help | Site Map

XSLT - Parameter value equals path to attribute, for query

Newbie
 
Join Date: Nov 2008
Posts: 4
#1: Nov 18 '08
Hi
I am very new to XSLT and have the following problem:
I would like to use a parameters value for the path to an attribute in my select query. For example:

I have an attribute named "pi" for the "date" element that sits in this path in my xml file:

visitors/visitor/date/@pi

so if I set the parameter:

<xsl:param name="myvar">date/@pi</xsl:param>

and create my select query to show each record with a date pi attribute with a value of 'Y':

<xsl:for-each select="visitors/visitor[$myvar = 'Y']">

it does not work as I was hoping???
I am probably thinking about things wrong, so if anyone could post any tips, that would be great.
Thanks,
Mat.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Nov 18 '08

re: XSLT - Parameter value equals path to attribute, for query


Expand|Select|Wrap|Line Numbers
  1. <xsl:param name="myvar">date/@pi</xsl:param>
$myvar is in this case a string (think of $myvar = 'date/@pi').

Expand|Select|Wrap|Line Numbers
  1. <xsl:param name="myvar" select="date/@pi"/>
will return a not-empty node/node-set if (and only if) the used xpath matches a node at the time of defining the parameter.

rule of thumb: <param> and <variable> are either empty, a string or a node-set. you cannot use them as part of an xpath-string like "/node()/$param/node()"

regards
Newbie
 
Join Date: Nov 2008
Posts: 4
#3: Nov 19 '08

re: XSLT - Parameter value equals path to attribute, for query


Thanks for your reply. I think I understand!
I am currently passing the value of parameters from javascript using the following method:

xslProc.addParameter("myvar", "date/@pi");

This would add "date/@pi" to the value of $myvar, not to the select value of $myvar.

How would I achieve this?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Nov 19 '08

re: XSLT - Parameter value equals path to attribute, for query


I don't think that's correct, but I admit I don't use xslt via javascript.

I assume that the expression of $myvar is empty when you set the parameter.
add somewhere in your xsl: <xsl:value-of select="$myvar"/> to check for the content of $myvar.

furthermore, visitors/visitor[$myvar = 'Y'] will imho never work as intended, because you can't save an xpath in a variable (which you'd need for that, you only have string, number or node-set available).

Quote:

Originally Posted by mrdancu

I would like to use a parameters value for the path to an attribute in my select query.

to answer that clearly, you can't do that.


regards

PS: I wish I could save an xpath in a variable, would be very useful.....
Newbie
 
Join Date: Nov 2008
Posts: 4
#5: Nov 19 '08

re: XSLT - Parameter value equals path to attribute, for query


Thanks again for the reply... I think you have confirmed what I thought, I can't pass an xpath query from javascript to an xslt file.
So I probably need to rethink what i am trying to acheive:

Basically I have a xml file with many visitor records (path: visitors/visitor) that have a date element with differing attributes.
Some will have an @pi1 = 'Y', some will have an @pi2 = 'Y', @pi3 = 'Y' etc etc etc up to @pi8.

What I would like to do is select the records based on whether they have a specific attribute or not.
I was trying to use javascript to achieve this with 1 xslt template and pass parameters to the xslt, but I may just have to use more than 1 template with a fixed query and vary the template used in the javascript.

If you have any other suggestions, that would be great.

Thanks again.
Mat.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: Nov 19 '08

re: XSLT - Parameter value equals path to attribute, for query


you could try to get the parameter to hold the complete xpath.
Expand|Select|Wrap|Line Numbers
  1. // JS
  2. xslProc.addParameter("myvar", "//visitor[date/@pi1 = 'Y']");
  3.  
  4. // XSL
  5. <xsl:param name="myvar" select="//visitor[date/@pi1 = 'Y']"/>
  6. // using
  7. <xsl:for-each select="$myvar">
  8. // code...
  9. </xsl:for-each>
check with <value-of> what the variable holds.

regards
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#7: Nov 19 '08

re: XSLT - Parameter value equals path to attribute, for query


If you treat the parameter as a name, then you could pass in a string. This would assume that values were either "Y" or "N".

xslProc.addParameter("myvar", "pi1");

<xsl:param name="myvar" select="'pi1'"/>
<xsl:for-each select="visitors/visitor[.//@*[local-name() = $myvar and .='Y']">
Newbie
 
Join Date: Nov 2008
Posts: 4
#8: Nov 19 '08

re: XSLT - Parameter value equals path to attribute, for query


Thanks to both of you for you help. I seemed to have cracked it using the following code:

javascript:
Expand|Select|Wrap|Line Numbers
  1. xslProc.addParameter("myvar", "pi1");
XSLT:
Expand|Select|Wrap|Line Numbers
  1. <xsl:param name="myvar"/>  
  2. <xsl:for-each select="visitors/visitor[date/@*[local-name() = $myvar]]">
If the date element has a @pi1, the record gets included!
Reply