开发者

Is there a way to output the name of elements and attributes from an XML doc?

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:

&开发者_运维技巧lt;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


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.


Try this:

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

  <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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜