name attribute for <xsl:element>
Hi everyone!
I came across a little problem while trying to output an <xsl:element>. I think the easiest way to make myself clear is an example.
--- XML source document ---
<?xml version="1.0"?>
<root>
<node name="example">
<child name="baby1">Some text here</child>
</node>
<node name="example">
<child name="baby2">Some text here</child>
</node>
</root>
--- End XML source document ---
--- Desired XML output ---
<?xml version="1.0"?>
<examples>
<example>
<child name="baby1">Some text here</child>
</example>
<example>
<child name="baby2">Some text here</child>
</example>
</examples>
--- End desired XML output ---
So I tried to use the following stylesheet to get the name of the element from the @name attribute of the node.
--- XSL used ---
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="root"/>
</xsl:template>
<xsl:template match="root">
<examples>
<xsl:for-each select="node">
<xsl:apply-templates select="."/>
</xsl:for-each>
</examples>
</xsl:template>
<xsl:template match="node">
<!--
HERE I try to name the <xsl:element>
using the @name of the current node
-->
<xsl:element name="@name">
<xsl:apply-templates select="child::node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="child">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
--- End XSL used ---
Sadly, this fails... So I also tried to put the @name in an <xsl:variable name="var"> and use $var to name the <xsl:element> with no success either.
I found a workaround using <xsl:text> but I just wonder if there'a a more elegant way to make this work.
Thanks in advance,
RushMan
Dijkstra's law on Programming and Inertia:
If you don't know what your program is supposed to do, don't try to write it.
__________________
Dijkstra's law on Programming and Inertia:
If you don't know what your program is supposed to do, don't try to write it.
|