10

having the following XML sample document, I need to issue an XPath/XQuery expression to get the element names for every children of a CD element.

<CD>
  <TITLE>Empire Burlesque</TITLE>
  <ARTIST>Bob Dylan</ARTIST>
  <COUNTRY>USA</COUNTRY>
  <COMPANY>Columbia</COMPANY>
  <PRICE>10.90</PRICE>
  <YEAR>1985</YEAR>
</CD>

So I need the query to return TITLE, ARTIST, COUNTRY, COUNTRY, PRICE, YEAR , any one can help please? thanks

michael
  • 1,577
  • 1
  • 12
  • 18
Lucy
  • 471
  • 4
  • 12
  • 28
  • Your terminology doesn't match your sample. ``, etc are child elements of `<cd>`, not attributes.</cd> – MattH Jun 20 '12 at 13:56

2 Answers2

19
/CD/*/name()

(padded out because StackOverflow doesn't like short answers)

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • what if we wanted to return CD? just name()? ./name() returns blank...trying to get the root node's name – tenwest May 08 '15 at 02:34
  • 1
    name(/*) - the outermost element is not the "root node" in the XPath data model; it has a parent which is called the root node in XPath 1.0, or the document node in XPath 2.0. – Michael Kay May 08 '15 at 15:11
  • javax.xml.transform.TransformerException: Unknown nodetype: name .. it is not working .. – ChanGan Jun 17 '20 at 09:10
  • The expression requires XPath 2.0+ or XQuery 1.0+, it won't work in XPath 1.0. – Michael Kay Jun 17 '20 at 20:01
-1

Try /\*/name()
e.g. for Oracle use

XMLCast(XMLQuery('/*/name()' PASSING db_some_field_name RETURNING CONTENT) AS VARCHAR2(4000))
sudo bangbang
  • 27,127
  • 11
  • 75
  • 77