您的模板看起來不錯,但是您確定要使用following
?例如,這個模板:
<xsl:template match="a">
<a><xsl:apply-templates select="m[following::y]"/></a>
<b><xsl:apply-templates select="m[following-sibling::y]"/></b>
</xsl:template>
...應用於以下XML:
<a>
<x/>
<m>match</m>
<y/>
<m>no match</m>
<nested>
<m>match 2</m>
<y/>
</nested>
</a>
...輸出以下結果:
<a>matchno match</a>
<b>match</b>
第一apply-templates
比賽<m>no match</m>
,因爲following
包括文檔順序中上下文節點之後的所有節點,其中包括嵌套的<y/>
。
第二個模板僅匹配兄弟。
爲了完整起見,我將添加下面的模板,只匹配那些<m>
節點,其立即下面的兄弟是<y>
:
<xsl:template match="a">
<a><xsl:apply-templates select="m[following-sibling::*[1][self::y]]"/></a>
</xsl:template>
此模板的以下輸出給出了上述XML:
<a>match</a>
+1對於「跟隨」和「跟隨兄弟姐妹」的差異。 – 2011-03-17 16:22:27