当前位置: 动力学知识库 > 问答 > 编程问答 >

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>

网友答案:

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....

网友答案:
<xsl:element name="../node-name()">
 <xsl:value-of select="."/>
</xsl:element>
分享给朋友:
您可能感兴趣的文章:
随机阅读: