Zeedware
Zeedware

Reputation: 53

How do I get element name in xslt/xquery

For example I have this XML

<?xml version="1.0" encoding="UTF-8"?>
<employee>
   <JOHN>
      <branch>0</branch>
   </JOHN>
   <ANDY>
      <branch>0996</branch>
   </ANDY>
   <ADAM>
      <branch>0996</branch>
   </ADAM>
</employee>

How can I get value of each element using XQuery? I want to get get

JOHN
ANDY
ADAM

Upvotes: 1

Views: 486

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167436

Using the XPath expression /employee/*/name() you will (with XSLT 2.0 or later or with XQuery 1.0 or later) get a sequence of strings with the element names. In XSLT 1.0 you would need to process /employee/* using for-each or apply-templates and then output the name() for each element processed separately.

So a complete XSLT 2.0 stylesheet is

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="/">
        <xsl:value-of select="/employee/*/name()"/>
    </xsl:template>
</xsl:transform>

Online sample at http://xsltransform.net/ej9EGc7.

Upvotes: 1

Related Questions