Groups | Blog | Home
all groups > dotnet xml > june 2004 >

dotnet xml : XSLT: Xpath with variables


Mike Grass
6/14/2004 11:24:01 PM
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,
Oleg Tkachenko [MVP]
6/15/2004 9:54:30 AM
[quoted text, click to view]

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.

[quoted text, click to view]

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]
Jay B. Harlow [MVP - Outlook]
6/16/2004 9:23:45 AM
Mike,
I haven't tried it, would a Custom Extension function work?

http://support.microsoft.com/default.aspx?scid=kb;en-us;324899

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

Hope this helps
Jay

[quoted text, click to view]

Sonu Kapoor
6/16/2004 7:14:22 PM
You could use a variable in the xml file and send the
xpath query to the xml file.

Sonu
[quoted text, click to view]
AddThis Social Bookmark Button