Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Creating a node in the output tree using a variable

Reply
Thread Tools

Creating a node in the output tree using a variable

 
 
Mike Conmackie
Guest
Posts: n/a
 
      04-28-2004
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 xmlnssl="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="." />
 
Reply With Quote
 
 
 
 
Ben Edgington
Guest
Posts: n/a
 
      04-28-2004
Hi,

"Mike Conmackie" <(E-Mail Removed)> 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 xmlnssl="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>
<xsltherwise>
<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 -->

</xsltherwise>
</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/
 
Reply With Quote
 
 
 
 
Mike Conmackie
Guest
Posts: n/a
 
      04-28-2004
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


 
Reply With Quote
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      04-29-2004
In article <408fccc9$1@10.10.0.241>,
Mike Conmackie <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
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
find path from one tree node to another tree node Peter Mueller Java 6 01-13-2008 02:36 AM
Null parent node on custom tree node after populate on demand John Bankhead ASP .Net Web Controls 0 12-04-2006 06:29 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM



Advertisments