从xquery中的xml文档中获取元素类型

时间:2012-06-20 11:57:35

标签: xpath xquery

我有以下xml文档,我需要一个xquery表达式来了解文档中有多少元素类型

    <category>
    <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
</CD>
<CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
</CD>
<CD>
  <book id="bk101">
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications 
  with XML.</description>
 </book>

 </category>

因此,我需要从查询中获取的返回值为cd and book。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

使用XQuery(或XPath 2.0)distinct-values(/category/*/local-name())就足够了,结果是一串字符串值CD, book