473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Creating a node in the output tree using a variable

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="." />
Jul 20 '05 #1
3 1938
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/
Jul 20 '05 #2
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
Jul 20 '05 #3
In article <40********@10.10.0.241>,
Mike Conmackie <mi**************@SPAMcompuware.com> 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
pt**@interlog.com
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
1
by: Doug | last post by:
I need to compare two "address" structures within a document, and perform some action if they are not equal. The XML document is a purchase order, with an address at both the header and line...
7
by: Rolf Kemper | last post by:
Dear All, somehow I remember that such or similar question was discussed already somewhere. But I can't find it anymore. I have a template calling itself. As long it goes deeper into the...
8
by: Ryan Stewart | last post by:
Putting the following code in a page seems to make it go into an infinite loop unless you give it a very simple node to work with. Either that or it's very very slow. I'm somewhat new to this,...
2
by: Andy Dingley | last post by:
I have a publishing application. The database layer queries various sources and produces an XML document, then XSLT processes this into HTML or RSS. In this particular case, there are several...
3
by: Blaise Garant | last post by:
Hi I've made a stylesheet to transform my data into XSL-FO. This stylesheet used to work with MSXSL 4.0 but I've got some issues in ..NET. First, I changed removed all the "node-set()" function...
4
by: Amit Bhatia | last post by:
User-Agent: OSXnews 2.081 Xref: number1.nntp.dca.giganews.com comp.lang.c++:817424 Hi, I have posted this post also for the thread "vector of lists" but since it is about something else...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.