473,394 Members | 2,071 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,394 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 8208
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

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

Similar topics

2
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...
1
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...
4
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
6
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'/>...
3
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...
7
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...
4
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...
1
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...
0
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...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.