XSLT variable path selection.

Discussion in 'XML' started by Eric Anderson, Dec 20, 2004.

  1. 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
     
    Eric Anderson, Dec 20, 2004
    #1
    1. Advertising

  2. 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
     
    Chris Huebsch, Dec 20, 2004
    #2
    1. Advertising

  3. 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
     
    Eric Anderson, Dec 20, 2004
    #3
  4. In article <>,
    Eric Anderson <> 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
     
    Richard Tobin, Dec 20, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Niederberger
    Replies:
    2
    Views:
    16,797
    Christian Kaufhold
    Jan 7, 2005
  2. Andrew Crowe
    Replies:
    1
    Views:
    4,550
    Andrew Crowe
    Sep 13, 2004
  3. Benjamin Hillsley
    Replies:
    3
    Views:
    1,747
    Dimitre Novatchev
    Sep 25, 2003
  4. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    291
    curiousity
    Nov 21, 2003
  5. Loebb
    Replies:
    0
    Views:
    218
    Loebb
    Feb 23, 2004
Loading...

Share This Page