Mottie
Mottie

Reputation: 86403

Getting the node name of an unknown XML node

I have an XML stylesheet with variable child nodes, something like this:

<fooz>
 <foo>
  <bar id="11">
 </foo>
 <foo>
  <baz id="22">
 </foo>
 <foo>
  <biz id="33">
 </foo>
</fooz>

So I know I can get the id's without any problems using

<xsl:value-of select="foo/*/@id"/>

But I'm kind of lost when it comes to getting the node name like "bar", "baz" or "biz". I tried stuff like this without any happy results:

<xsl:value-of select="foo/node()"/>

Is there an easy way to do this?

Upvotes: 0

Views: 11806

Answers (1)

Maarten Kesselaers
Maarten Kesselaers

Reputation: 1251

I've tried the first xsl-statement with your xml-example, but I couldn't retrieve all the id-attributes with it, only the first. To get all at once, I needed to use a for-each statement.

To get back the name of a node, you can use :

<xsl:value-of select="local-name()"/>

or

<xsl:value-of select="name()"/>

To get all names under the foo elements, I've come up with the following :

<xsl:for-each select="/fooz/foo/*">
  <tr>
    <td><xsl:value-of select="local-name()"/></td>
    <td><xsl:value-of select="@id" /></td>
  </tr>
</xsl:for-each>

Upvotes: 3

Related Questions