简体   繁体   English

XSLT将子节点文本复制到父节点

[英]XSLT Copy child nodes text to parent node

I need help in following xslt code. 我需要以下xslt代码的帮助。 I have input as: 我输入为:

 <book>
      <book1>
           <name>abc</name>
           <revision>1</revision>
      </book1>
      <book2>
           <name>pqr</name>
           <author>def</author>
      </book2>
 </book>

My expected output as: 我的预期输出为:

  <book>
      <item>
           <name>book1</name>
           <value>abc1</value>
      </item>
      <item>
           <name>book2</name>
           <value>pqrdef</value>
      </item>
 </book>

I have tried fetching value for value node using */text() but i get text only from first child. 我尝试使用* / text()为值节点获取值,但是我仅从第一个孩子得到文本。 In future I have many such child elements. 将来我会有很多这样的子元素。

Thanks in advance. 提前致谢。

Regards, Minakshi 此致Minakshi

This stylesheet will give you what you want. 该样式表将为您提供所需的内容。 Even if the child of boonK element increase, the template will not need to be change. 即使boonK元素的子元素增加,也无需更改模板。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:template match="book">
        <xsl:copy>
            <xsl:apply-templates select="*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="book/*">
        <item>
            <name>
                <xsl:value-of select="name()"/>
            </name>
            <value>
                <xsl:for-each select="*">
                    <xsl:value-of select="."/>
                </xsl:for-each>
            </value>
        </item>
    </xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM