Creating a node in the output tree using a variable

M

Mike Conmackie

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="." />
 
B

Ben Edgington

Hi,

Mike Conmackie said:
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.
<!-- 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
 
M

Mike Conmackie

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
 
P

Patrick TJ McPhee

% 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.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top