473,385 Members | 1,707 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,385 software developers and data experts.

XSLT: Xpath with variables

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'/>
<options>
<USER>
<NAME>Bob</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Jane</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Bill</NAME>
<id>type2</id>
</USER>
<!-- ... -->
</options>
<!-- end snippet -->

What I would like to do using xslt is for each <selector> tag, look up
the tag(s) in <options> whose ids match the value of the node
distinguished by the selector's 'key' attribute, e.g. for the first
selector, I would like a node-set containing the users Bob and Jane, but
not Bill.

I need the path to the node (the selector 'key') to be specified in the
XML document, so I can't do it statically like:
<xsl:variable name="value" select="selector/@value"/>
<xsl:for-each select='options/USER/id=$value'>...</xsl:for-each>

I believe that also means I can't use xsl:key, since it must be
top-level and can't have variable names in its match attribute.

Is this possible with xslt or should I seek some other solution?

Regards,
--Mike
Nov 12 '05 #1
6 4653
Mike Grass wrote:
I need the path to the node (the selector 'key') to be specified in the
XML document
That's usually bad idea. XPath and XSLT don't support dynamic evaluation
of XPath expressions, so you task at ones becomes quite onerous just
because of poor design. Consider not using XPath in your data, use
traditional XML means to express relationship - hierarchy, element
names, IDs etc.
Is this possible with xslt or should I seek some other solution?


I'd go for two-step transformation - first generate XSLT stylesheet
(using XSLT again of course) with appropriate xsl:key definitions
according to selector keys and then run it.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
Mike Grass wrote:
I need the path to the node (the selector 'key') to be specified in the
XML document
That's usually bad idea. XPath and XSLT don't support dynamic evaluation
of XPath expressions, so you task at ones becomes quite onerous just
because of poor design. Consider not using XPath in your data, use
traditional XML means to express relationship - hierarchy, element
names, IDs etc.
Is this possible with xslt or should I seek some other solution?


I'd go for two-step transformation - first generate XSLT stylesheet
(using XSLT again of course) with appropriate xsl:key definitions
according to selector keys and then run it.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #3
Mike,
I haven't tried it, would a Custom Extension function work?

http://support.microsoft.com/default...b;en-us;324899

Otherwise I agree with Oleg, I would find a different method to use...

Hope this helps
Jay

"Mike Grass" <mi**@accessdata.com> wrote in message
news:ef**************@TK2MSFTNGP11.phx.gbl...
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'/>
<options>
<USER>
<NAME>Bob</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Jane</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Bill</NAME>
<id>type2</id>
</USER>
<!-- ... -->
</options>
<!-- end snippet -->

What I would like to do using xslt is for each <selector> tag, look up
the tag(s) in <options> whose ids match the value of the node
distinguished by the selector's 'key' attribute, e.g. for the first
selector, I would like a node-set containing the users Bob and Jane, but
not Bill.

I need the path to the node (the selector 'key') to be specified in the
XML document, so I can't do it statically like:
<xsl:variable name="value" select="selector/@value"/>
<xsl:for-each select='options/USER/id=$value'>...</xsl:for-each>

I believe that also means I can't use xsl:key, since it must be
top-level and can't have variable names in its match attribute.

Is this possible with xslt or should I seek some other solution?

Regards,
--Mike

Nov 12 '05 #4
Mike,
I haven't tried it, would a Custom Extension function work?

http://support.microsoft.com/default...b;en-us;324899

Otherwise I agree with Oleg, I would find a different method to use...

Hope this helps
Jay

"Mike Grass" <mi**@accessdata.com> wrote in message
news:ef**************@TK2MSFTNGP11.phx.gbl...
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'/>
<options>
<USER>
<NAME>Bob</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Jane</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Bill</NAME>
<id>type2</id>
</USER>
<!-- ... -->
</options>
<!-- end snippet -->

What I would like to do using xslt is for each <selector> tag, look up
the tag(s) in <options> whose ids match the value of the node
distinguished by the selector's 'key' attribute, e.g. for the first
selector, I would like a node-set containing the users Bob and Jane, but
not Bill.

I need the path to the node (the selector 'key') to be specified in the
XML document, so I can't do it statically like:
<xsl:variable name="value" select="selector/@value"/>
<xsl:for-each select='options/USER/id=$value'>...</xsl:for-each>

I believe that also means I can't use xsl:key, since it must be
top-level and can't have variable names in its match attribute.

Is this possible with xslt or should I seek some other solution?

Regards,
--Mike

Nov 12 '05 #5
You could use a variable in the xml file and send the
xpath query to the xml file.

Sonu
-----Original Message-----
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'/>
<options>
<USER>
<NAME>Bob</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Jane</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Bill</NAME>
<id>type2</id>
</USER>
<!-- ... -->
</options>
<!-- end snippet -->

What I would like to do using xslt is for each <selector> tag, look upthe tag(s) in <options> whose ids match the value of the nodedistinguished by the selector's 'key' attribute, e.g. for the firstselector, I would like a node-set containing the users Bob and Jane, butnot Bill.

I need the path to the node (the selector 'key') to be specified in theXML document, so I can't do it statically like:
<xsl:variable name="value" select="selector/@value"/>
<xsl:for-each select='options/USER/id=$value'>...</xsl:for-each>
I believe that also means I can't use xsl:key, since it must betop-level and can't have variable names in its match attribute.
Is this possible with xslt or should I seek some other solution?
Regards,
--Mike
.

Nov 12 '05 #6
You could use a variable in the xml file and send the
xpath query to the xml file.

Sonu
-----Original Message-----
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'/>
<options>
<USER>
<NAME>Bob</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Jane</NAME>
<id>type1</id>
</USER>
<USER>
<NAME>Bill</NAME>
<id>type2</id>
</USER>
<!-- ... -->
</options>
<!-- end snippet -->

What I would like to do using xslt is for each <selector> tag, look upthe tag(s) in <options> whose ids match the value of the nodedistinguished by the selector's 'key' attribute, e.g. for the firstselector, I would like a node-set containing the users Bob and Jane, butnot Bill.

I need the path to the node (the selector 'key') to be specified in theXML document, so I can't do it statically like:
<xsl:variable name="value" select="selector/@value"/>
<xsl:for-each select='options/USER/id=$value'>...</xsl:for-each>
I believe that also means I can't use xsl:key, since it must betop-level and can't have variable names in its match attribute.
Is this possible with xslt or should I seek some other solution?
Regards,
--Mike
.

Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johannes Lebek | last post by:
Hi there, somehow, I cannot access nodes that are stored in a variable. I'm using Xalan 2.5.1 and the following commands: ================ BEGIN ==================== <xsl:variable...
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
5
by: Claudio Jolowicz | last post by:
Suppose you have this document: <root> <node id="1"> <node id="4"/> <node id="5"/> </node> <node id="2"/> <node id="3"/> </root>
1
by: Oleg Konovalov | last post by:
Hi, I am new to XSLT, trying to significantly modify somebody else's XSL. That is not 2.0. I need to create min & max variable(s) to be used in many templates and sub-templates based on...
3
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
1
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
0
by: SamStamport | last post by:
Hello, I have coded a C# program that passes an argument list to my XSLT file. I get no errors, but I don't get any output either. Here's what I want to do: pass a parameter that is an XPath...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
0
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,...
0
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$) { } ...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.