Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT variable path selection.

Reply
Thread Tools

XSLT variable path selection.

 
 
Eric Anderson
Guest
Posts: n/a
 
      12-20-2004
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
 
Reply With Quote
 
 
 
 
Chris Huebsch
Guest
Posts: n/a
 
      12-20-2004
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
--
Chris Huebsch www.hübsch-gemacht.de | TU Chemmnitz, Informatik, RNVS
GPG-Encrypted mail welcome! ID:7F2B4DBA | Str. d. Nationen 62, B204
Chemnitzer Linux-Tage 2005, 5.-6.März | D-09107 Chemnitz
http://chemnitzer.linux-tage.de/ | +49 371 531-1377, Fax -1803
 
Reply With Quote
 
 
 
 
Eric Anderson
Guest
Posts: n/a
 
      12-20-2004
Chris Huebsch wrote:
> 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
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      12-20-2004
In article <(E-Mail Removed)>,
Eric Anderson <(E-Mail Removed)> wrote:

><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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including XSLT/XML document within a XSLT document dar_imiro@hotmail.com XML 4 12-13-2005 02:26 AM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments