473,374 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XSLT - Parameter value equals path to attribute, for query

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:


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.
Nov 18 '08 #1
7 8204
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()"

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
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.


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.
Nov 19 '08 #5
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']");
  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.

Nov 19 '08 #6
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:

Expand|Select|Wrap|Line Numbers
  1. xslProc.addParameter("myvar", "pi1");
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

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

Similar topics

by: Jon Martin Solaas | last post by:
For java programmers there exist a framework called Millstone (www.millstone.org) for programming web user-interfaces in a component oriented way. Millstone uses xslt transformations to render...
by: tranvirus | last post by:
Hi, I have these two XML documents that I'm trying to merge, they are identical except by the content they have is in different languages so I want to generate a third document with a new format...
by: ak | last post by:
Hi- Is it possible to use XSLT (e.g. using xslLsort and xsl:output), to output/write a sorted XML file? Don't want an HTML browser rendering, need a sorted XML file. Thanks. ak
by: Mike Grass | last post by:
Hi, I have an XML file similar to the following: <!-- snippet --> <selector key='USER/id' value='type1'/> <selector key='USER/id' value='type2'/> <selector key='USER/id' value='type3'/>...
by: Gordon Moore | last post by:
Hi, I'm new to using xml/xslt and although I can create an xml document using the dataset.WriteXml statement, and I have created an xslt to transform the xml into the output I want, I have to...
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
by: David S. Alexander | last post by:
How can I do simple subtraction in an XSLT. I want to read a few attribute values from an XML document, calculate their difference, and transform that value to an attribute in the XML output...
by: boetke | last post by:
Hi all, I am attempting to create an xslt transformation which I am having trouble with. I am fairly new to xslt and am having a hard time getting my head around it. The original xml file looks...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.