eitann
eitann

Reputation: 1270

Getting xml parent node tag name with child node value using XSLT

I need to transform XML using xslt.

i want to transform a node that has child nodes, to node that his tag name is the name of the parent and his value is the value of one of his children.

example:

the given xml:

<Parent>
  <ChildA>1</ChildA>
  <ChildB>2</ChildB>
</Parent>

the desired xml output:

<Parent>2</Parent>

Upvotes: 0

Views: 1932

Answers (2)

MiMo
MiMo

Reputation: 11983

You are not specifying which child node you are interested in....

Something like this:

<xsl:template match="Parent">
  <Parent>
    <xsl:value-of select="ChildB"/>
  </Parent>
</xsl:template>

uses the value of the first child node called ChildB (if any). This:

<xsl:template match="Parent">
  <Parent>
    <xsl:value-of select="*[2]"/>
  </Parent>
</xsl:template>

uses the value of the second child node. This:

<xsl:template match="Parent">
  <Parent>
    <xsl:value-of select="*[last()]"/>
  </Parent>
</xsl:template>

uses the value of the last child node....

Upvotes: 2

Treemonkey
Treemonkey

Reputation: 2163

<xsl:element name="../node-name()">
 <xsl:value-of select="."/>
</xsl:element>

Upvotes: 0

Related Questions