XSLT variable path selection.

E

Eric Anderson

Suppose I have a variable which holds a tag name. I then want to be able
to select that tag name. How do I do that? For example:

<xsl:variable name="tag" select="'foo'"/>
<xsl:copy-of select="/ns:baz/ns:$tag"/>

This should copy the element /ns:baz/ns:foo. I have tried a couple of
ideas but none seem to work. Some that I have tried are:

<xsl:copy-of select="/ns:baz[name(current()) = concat('ns:', $tag)]"/>

and

<xsl:copy-of select="/ns:baz[name(*) = concat('ns:', $tag)]"/>

I pretty much understand why those didn't work, but I am not really sure
what will work. I assume it is something extremely simple and obvious
but I drawing a blank.

Thank you for any help you can provide.

Eric
 
C

Chris Huebsch

Eric Anderson (Mon, 20 Dec 2004 17:13:10 -0500):
Suppose I have a variable which holds a tag name. I then want to be able
to select that tag name. How do I do that? For example:

I used the exslt:dynamic-evaluate function for that.

Don't know if this is "oversized" and if your xslt-parser supports it,
but in my case it looked nice.


Chris
 
E

Eric Anderson

Chris said:
I used the exslt:dynamic-evaluate function for that.

Don't know if this is "oversized" and if your xslt-parser supports it,
but in my case it looked nice.

This will be sent directly to a browser so I don't think IE's MSXML lib
supports exslt or Mozilla's Transformix lib. Any other ideas? My only
thought is to loop through all the children of the parent and compare
the name of current() to the name I am looking for. But this is
obviously not as efficient as a XPath query.

Eric
 
R

Richard Tobin

Eric Anderson said:
<xsl:variable name="tag" select="'foo'"/>
<xsl:copy-of select="/ns:baz/ns:$tag"/>

You can't use variables like that.
<xsl:copy-of select="/ns:baz[name(current()) = concat('ns:', $tag)]"/>

This compares the name of the current node with concat('ns:', $tag),
not the name of the child of ns:baz.
<xsl:copy-of select="/ns:baz[name(*) = concat('ns:', $tag)]"/>

This selects an ns:baz that has a child with that name, not the child
itself.

You need something like

<xsl:copy-of select="/ns:baz/*[name() = concat('ns:', $tag)]"/>

but that relies on the prefix returned by name() being "ns", which is
not guaranteed. It would be better to test the namespace itself:

<xsl:copy-of select="/ns:baz/*[local-name() = $tag and
namespace-uri()='whatever-ns-is-bound-to']"/>

If you are confident there aren't any elements in other namespaces,
this will be sufficient:

<xsl:copy-of select="/ns:baz/*[local-name() = $tag]"/>

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top