tags:

views:

32

answers:

2

What I am trying to do is use XSL to output all unique element and attribute names. Not their values, but their names.

So given an XML of:

<item id="12">
  <price>12.00</price>
  <author>Name</author>
  <desc>Description</desc>
</item>

I want to show that there are elements of item,price,author,desc. In addition to that I want to know there is an attribute of 'id'.

Any ideas on how to do this? Or articles I can read about it? Is it even possible?

Thanks,
Levi

+2  A: 

I haven't used them a lot myself, but these functions should get you there: XPath functions on nodes. More specifically, look at name() and local-name(). Since they work on nodes, there should be no problem using them on elements as well as attributes.

Jakob
+1  A: 

Try this:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:key name="names" match="//* | //@*" use="name()"/>
  <xsl:template match="/">
    <xsl:for-each select="(//* | //@*)[count(key('names', name())) = 1]">
      <xsl:value-of select="name()" /><br />
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
Rubens Farias
More like `<xsl:for-each select="(//* | //@*)[count(. | key('names', name())[1]) = 1]">`, or am I misunderstanding the "unique" requirement?
Tomalak
I don't think so; I used name() when building 'names' key, so it probably wont match with .
Rubens Farias