468,249 Members | 1,513 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

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 4399
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Johannes Lebek | last post: by
5 posts views Thread by Claudio Jolowicz | last post: by
1 post views Thread by Oleg Konovalov | last post: by
3 posts views Thread by Ian Roddis | last post: by
1 post views Thread by Sergey Dubinets | last post: by
reply views Thread by SamStamport | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.