Creating a node in the output tree using a variable

Discussion in 'XML' started by Mike Conmackie, Apr 28, 2004.

  1. Greetings,

    I am trying to create a node in the output tree using a variable. Here are
    some fragments that I hope will explain the problem better.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:glbl="http://www.compuware.com/XSL/globalvariables"
    version="1.0">
    <glbl:host-id-types>
    <glbl:host-id-type t="undefined">unspecified</glbl:host-id-type>
    <glbl:host-id-type t="Eth">ethernet</glbl:host-id-type>
    <glbl:host-id-type t="Serial">serial</glbl:host-id-type>
    <glbl:host-id-type t="Softkey">softkey</glbl:host-id-type>
    </glbl:host-id-types>
    <xsl:key name="host-id-type-lookup" match="glbl:host-id-type" use="." />
     
    Mike Conmackie, Apr 28, 2004
    #1
    1. Advertising

  2. Hi,

    "Mike Conmackie" <> writes:
    > I am trying to create a node in the output tree using a variable. Here are
    > some fragments that I hope will explain the problem better.

    <snip/>
    > <!-- right here I need to create a node in the output tree as
    > follows:
    > if the input node was <flx:hostid
    > flx:type="ethernet">00045a447966</flx:hostid>
    > then the output node should be:
    > <Eth>00045a447966</Eth>
    > How can this be accomplished using the host-id-translated
    > variable? Do I use xsl:element
    > or xsl:value-of in some fashion?
    > -->

    <snip/>

    I fixed up your XSLT to do this. Whether it's the *best* way to
    do what you want I haven't thought yet...

    Look at my comments for the interesting bits.

    This XML:
    <flx:foo xmlns:flx="http://example.com/">
    <flx:hostid flx:type="ethernet">00045a447966</flx:hostid>
    <flx:hostid flx:type="serial">abcdefghijkl</flx:hostid>
    </flx:foo>


    and this XSLT:
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:glbl="http://www.compuware.com/XSL/globalvariables"
    xmlns:flx="http://example.com/"
    exclude-result-prefixes="glbl flx"
    version="1.0">
    <glbl:host-id-types>
    <glbl:host-id-type t="undefined">unspecified</glbl:host-id-type>
    <glbl:host-id-type t="Eth">ethernet</glbl:host-id-type>
    <glbl:host-id-type t="Serial">serial</glbl:host-id-type>
    <glbl:host-id-type t="Softkey">softkey</glbl:host-id-type>
    </glbl:host-id-types>
    <xsl:key name="host-id-type-lookup" match="glbl:host-id-type" use="." />
    <xsl:template match="//flx:hostid">
    <xsl:choose>
    <!-- NB I inserted the not() function here. It seems right -->
    <xsl:when test="not(boolean(@flx:type))">
    <Hostid>undefined</Hostid>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:variable name="host-type" select="@flx:type" />
    <!-- store the contents of the node so we have it after we
    change context with for-each -->
    <xsl:variable name="foo" select="." />
    <xsl:for-each select="document('')">

    <!-- I corrected the key name here -->
    <xsl:variable name="host-id-translated"
    select="key('host-id-type-lookup',$host-type)/@t" />

    <xsl:element name="{$host-id-translated}">
    <xsl:value-of select="$foo"/>
    </xsl:element>

    </xsl:for-each>

    <!-- You can't use $host-id-translated here - it's out of scope -->

    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    </xsl:stylesheet>


    give this output:
    <?xml version="1.0" encoding="UTF-8"?>
    <Eth>00045a447966</Eth>
    <Serial>abcdefghijkl</Serial>


    I hope that helps you.

    Ben

    --
    Ben Edgington
    Mail to the address above is discarded.
    Mail to ben at that address might be read.
    http://www.edginet.org/
     
    Ben Edgington, Apr 28, 2004
    #2
    1. Advertising

  3. Ben,

    Thank you very much for fixing the oversights in the template as well as
    answering my question. I didn't realize that the namespace(s) used in the
    input document also needed to be defined in the stylesheet. I thought that
    the XSLT processor could get the URI from the input tree. As for the "table
    look-up", I obviously fat fingered it because it was copied (conceptually)
    from one of your earlier posts regarding associative arrays.

    Regards,

    Mike
     
    Mike Conmackie, Apr 28, 2004
    #3
  4. In article <408fccc9$1@10.10.0.241>,
    Mike Conmackie <> wrote:

    % answering my question. I didn't realize that the namespace(s) used in the
    % input document also needed to be defined in the stylesheet. I thought that
    % the XSLT processor could get the URI from the input tree.

    It can, but if you want to match an element, you need to supply both the
    name and the namespace. The most convenient way to do that in XPath is
    to define a namespace prefix.

    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Apr 29, 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. Replies:
    0
    Views:
    1,671
  2. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,244
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,521
    Dimitre Novatchev
    Aug 24, 2006
  4. Peter Mueller
    Replies:
    6
    Views:
    4,722
    Stefan Ram
    Jan 13, 2008
  5. John Bankhead

    Null parent node on custom tree node after populate on demand

    John Bankhead, Dec 4, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    304
    John Bankhead
    Dec 4, 2006
Loading...

Share This Page