Hi,
"Mike Conmackie" <mi**************@SPAMcompuware.com> 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:otherwise>
<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:otherwise>
</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/