正如 Ian Roberts 所解释的,在 XSLT 中没有打破循环的概念。您必须提前决定要处理哪些节点,听起来您只对元素感兴趣,包括包含“2)”的第一个abc元素。
实际上,在这里使用xsl:apply-templates可能会更好。
<xsl:apply-templates select="abc[not(preceding-sibling::abc[contains(xyz, '2)')])]" />
因此,这将选择所有没有前面的 '2)' 元素的元素。
接下来,您可以根据xyz元素的内容添加模板以匹配abc元素。例如,要输出“XSL_1.0 Programming”,如果它有一个 '2)',请执行以下操作
<xsl:template match="abc[contains(xyz, '2)')]">
<xsl:text>XSL_1.0 Programming</xsl:text>
</xsl:template>
这是完整的 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:apply-templates select="abc[not(preceding-sibling::abc[contains(xyz, '2)')])]" />
</xsl:template>
<xsl:template match="abc">
<xsl:value-of select="concat(xyz, ' ')" />
</xsl:template>
<xsl:template match="abc[contains(xyz, '2)')]">
<xsl:text>XSL_1.0 Programming </xsl:text>
</xsl:template>
</xsl:stylesheet>
应用于以下 XML 时(注意它有一个根元素)
<root>
<abc>
<def>some text1</def>
<xyz>stack overflow</xyz>
</abc>
<abc>
<def>some text2</def>
<xyz>stack overflow 2)</xyz>
</abc>
<abc>
<def>some text3</def>
<xyz>stack overflow</xyz>
</abc>
<abc>
<def>some text</def>
<xyz>stack overflow 3)</xyz>
</abc>
</root>
以下是输出
stack overflow
XSL_1.0 Programming