Page 1 of 1

Convert element name to text

Posted: Fri May 16, 2008 1:36 am
by ted
Hi,

I'm hoping someone might be kind enough to tell me how to convert an element name to text.
A contrived, but simple example:

<LIST>
<Color>Blue</Color>
<Size>Small</Small>
</LIST>

converted to:

<LIST>
<ATTRIBUTE>
<TYPE>Color</TYPE>
<VALUE>Blue<VALUE>
</ATTRIBUTE>
<ATTRIBUTE>
<TYPE>Size</TYPE>
<VALUE>Small</VALUE>
</ATTRIBUTE>
</LIST>

Thanks very much,
T

Re: Convert element name to text

Posted: Fri May 16, 2008 8:42 am
by Radu
Hi Ted,

Here is a small XSL sample to get you going:

Code: Select all


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<LISTS>
<xsl:apply-templates/>
</LISTS>
</xsl:template>
<xsl:template match="LIST">
<ATTRIBUTE>
<TYPE><xsl:value-of select="local-name(Color)"/></TYPE>
<VALUE>Blue</VALUE>
</ATTRIBUTE>
<ATTRIBUTE>
<TYPE><xsl:value-of select="local-name(Size)"/></TYPE>
<VALUE>Small</VALUE>
</ATTRIBUTE>
</xsl:template>
</xsl:stylesheet>
Notice the local-name method used to get the node local name.
You can also use the node-name method to get the whole tag name (with prefix).

Regards,
Radu

Re: Convert element name to text

Posted: Sat May 17, 2008 6:53 pm
by ted
ah, local-name(), thats what I was after!
Thanks very much.