How does one use XSLT to output the name of an element or an attribute?

Peter Ciuffetti

What you need is something like this:

<xsl:template match="root">
<xsl:for-each select="*">
	<xsl:element name="{local-name(@*)}">
		<xsl:attribute name="{local-name()}">
			<xsl:value-of select="@*"/>
This uses the local-name() function of XPATH to obtain the name of the element and the name of the first attribute in that element. It will transform:
  <a b="c"/>
  <d e="f"/>
  <g h="i"/>
  <e d="f"></e>
  <h g="i"></h>
Note: When you pass local-name a node set (like @*), it returns only the name of the first node in document order. So you can't use this trick to obtain the name of a second or subsequent attribute.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact