The name of the first child node in xslt

I wanted to know how to find the first child node name of a specific node in xslt.

I have xml:

 <name>
    <body>
      <para>
        <text> some text</text>
      </para>
    </body>
  </name>

Can I get the name using body / node () [1] / local-name ()?

<xsl:template match="name">
<name> 
<xsl:variable name="firstchild" select="body/node()[1]/local-name()">
                        </xsl:variable>
 <xsl:value-of select="$firstchild" />
 </name>
</xsl:template>

The exit should be

 <name>
    para
  </name> 
+5
source share
1 answer

Try something like this ...

<xsl:template match="name">
  <name>
  <xsl:variable name="firstchild" select="name(body/*[1])"/>
  <xsl:value-of select="$firstchild" />
  </name>
</xsl:template>

Or, if you really don't need a variable, just ...

<xsl:template match="name">
  <name>
  <xsl:value-of select="name(body/*[1])" />
  </name>
</xsl:template>

Here is the xmlplayground of the second example ... to see <name>para</name>click on View Sourcein the output window.

+6
source

All Articles