您应该能够获取具有blue 的部分的@id,然后剥离section 并“解包”reference。
这里有 2 个例子。一个在 XSLT 1.0 中,一个在 XSLT 2.0 中。
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="toStrip" select="'blue'"/>
<xsl:variable name="stripRef" select="//section[color=$toStrip]/@id"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="header">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::section[@id=$stripRef])]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="reference">
<xsl:choose>
<xsl:when test="@link=$stripRef">
<xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="toStrip" select="'blue'"/>
<xsl:variable name="stripRef" select="//section[color=$toStrip]/@id"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="section[@id=$stripRef]"/>
<xsl:template match="reference[@link=$stripRef]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
根据评论进行编辑
这是处理多个 ID 的一种方法。由于toStrip 参数不能是序列,我们需要添加一个字符来分隔值。在这个例子中,我使用了|。这样在比较 a1 和 a10 之类的内容时,contains() 就不会是真的。
此外,XSLT 1.0 将仅返回 stripRef 变量选择中的第一个 @id。为了获取所有 ID,我添加了一个 xsl:for-each。您也可以使用模式对xsl:apply-templates 执行此操作。
这是更新后的 XSLT:
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="toStrip" select="'|blue|orange|'"/>
<xsl:variable name="stripRef">
<xsl:text>|</xsl:text>
<xsl:for-each select="//section[color[contains($toStrip,concat('|',.,'|'))]]/@id">
<xsl:value-of select="concat(.,'|')"/>
</xsl:for-each>
</xsl:variable>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="header">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::section[contains($stripRef,concat('|',@id,'|'))])]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="reference">
<xsl:message>contains(<xsl:value-of select="$stripRef"/>,<xsl:value-of select="concat('|',@link,'|')"/>)</xsl:message>
<xsl:choose>
<xsl:when test="contains($stripRef,concat('|',@link,'|'))">
<xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
要在 XSLT 2.0 中做同样的事情,您只需将 toStrip 参数更改为一个序列:
<xsl:param name="toStrip" select="('blue','orange')"/>