您的模板看起来不错,但是您确定要使用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