Convert element name to text

Here should go questions about transforming XML with XSLT and FOP.
ted
Posts: 2
Joined: Fri May 16, 2008 1:31 am

Convert element name to text

Post 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
Radu
Posts: 9438
Joined: Fri Jul 09, 2004 5:18 pm

Re: Convert element name to text

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
ted
Posts: 2
Joined: Fri May 16, 2008 1:31 am

Re: Convert element name to text

Post by ted »

ah, local-name(), thats what I was after!
Thanks very much.
Post Reply