【发布时间】:2017-07-11 13:52:06
【问题描述】:
我需要能够根据段落的子元素<version> 中包含的属性来删除特定的父节点(及其子节点)。因此,在下面的示例中,我需要 XSLT 来查找 <version version="ABCD"> 的实例并从父 <para0> 元素中删除所有内容。换句话说,我正在尝试完成条件文本过滤。我需要匹配(和删除)的元素将始终是 <applic> 的父元素,但可能并不总是像示例中那样是 <para0>,因此我需要以某种方式指定它需要匹配“应用程序”的父元素' 元素而不是显式指定 para0。
从示例中应该更清楚。我需要删除所有版本属性为 ABCD 的 para0 数据。
这是一些示例 XML
<root>
<para0>
<applic>
<model>
<version version="ABCD"></version>
</model>
</applic>
<subpara1><title>First Title</title>
<para>Some text relating to ABCD configuration</para>
</subpara1>
<subpara1><title>Second Title</title>
<para>Some other text and stuff relating to ABCD configuration</para>
</subpara1>
</para0>
<para0>
<applic>
<model>
<version version="TRAINING"></version>
</model>
</applic>
<subpara1><title>First Title</title>
<para>Some text relating to TRAINING configuration</para>
</subpara1>
<subpara1><title>Second Title</title>
<para>Some other text and stuff relating to TRAINING configuration</para>
</subpara1>
</para0>
</root>
这是我目前拥有的 XSLT,但我需要它,一旦与 ABCD 匹配,基本上选择并删除“应用”的父节点和所有子节点。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method = "xml" indent="yes"/>
<xsl:strip-space elements = "*" />
<xsl:template match = "@*|node()" >
<xsl:copy>
<xsl:apply-templates select = "@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match = "//applic/model[@*]/version[@version='ABCD']" />
</xsl:stylesheet>
【问题讨论】: