首页 > 解决方案 > XSLT 删除同名节点中的节点

问题描述

我有一个 XML 文档。

para在其他节点中有大量para​​节点。

我想删除里面的那些并保持内部文本将它连接到其他para节点的内部文本。

示例 XML

<document>
<head>
<front>The front page</front>
</head>
<h1>
<para id="1234">This is the inner text <para> it needs joining together</para> maybe with other text</para>
</h1>
</document>

期望的输出

<document>
<head>
<front>The front page</front>
</head>
<h1>
<para id='1234'>This is the inner text it needs joining together maybe with other text</para>
</h1>
</document>

标签: xmlxsltnodes

解决方案


这是相当微不足道的:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="para/para">
    <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

推荐阅读