472,096 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

XSLT - Parameter value equals path to attribute, for query

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.
Nov 18 '08 #1
7 8047
Dormilich
8,658 Expert Mod 8TB
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
Nov 18 '08 #2
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?
Nov 19 '08 #3
Dormilich
8,658 Expert Mod 8TB
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).

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.....
Nov 19 '08 #4
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.
Nov 19 '08 #5
Dormilich
8,658 Expert Mod 8TB
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
Nov 19 '08 #6
jkmyoung
2,057 Expert 2GB
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']">
Nov 19 '08 #7
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!
Nov 19 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jon Martin Solaas | last post: by
1 post views Thread by tranvirus | last post: by
4 posts views Thread by ak | last post: by
6 posts views Thread by Mike Grass | last post: by
7 posts views Thread by Harolds | last post: by
4 posts views Thread by David S. Alexander | last post: by
1 post views Thread by boetke | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.