我使用该输入XML文件。
<Content>
<body><text>xxx</text></body>
<body><text>yy</text></body>
<body><text>zz</text></body>
<body><text>kk</text></body>
<body><text>mmm</text></body>
</Content>
XSLT转换后输出应该是
<Content>
<body><text>xxx</text>
<text>yy</text>
<text>zz</text>
<text>kk</text>
<text>mmm</text></body>
</Content>
任何人都可以请提供其初步认识XSL文件。
这完全转变 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body"/>
<xsl:template match="body[1]">
<body>
<xsl:apply-templates select="../body/node()"/>
</body>
</xsl:template>
</xsl:stylesheet>
当施加在提供的XML文档:
<Content>
<body>
<text>xxx</text>
</body>
<body>
<text>yy</text>
</body>
<body>
<text>zz</text>
</body>
<body>
<text>kk</text>
</body>
<body>
<text>mmm</text>
</body>
</Content>
产生想要的,正确的结果 :
<Content>
<body>
<text>xxx</text>
<text>yy</text>
<text>zz</text>
<text>kk</text>
<text>mmm</text>
</body>
</Content>
说明 :
该标识规则副本的每个节点“原样”。
它由两个模板覆盖。 首先忽略/删除每一个body
element`。
第二个模板覆盖的身份模板还覆盖了第一个这样的模板(即删除每一个body
部件),用于任何body
是第一要素body
其父的孩子。 对于这第一个body
子止, body
生成元素,并在其主体是任何的子节点的所有节点body
其父子(电流body
元件和其所有的body
兄弟姐妹)进行处理。
<xsl:template match="Content">
<body>
<xsl:apply-templates select="body/text"/>
</body>
</xsl:template>
<xsl:template match="body/text">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
文章来源: how to get a transformed xml file with all child tags of every occurance of parent tag under only one parent tag