全分辨率过程在section 5.5 of the XSLT spec中描述。
在一般情况下,适用下列规则,以便(例如,由于较低的导入优先排除在考虑之外的模板被永久淘汰,而不管其优先级):
- 导入的模板比在模板中的优先级较低初始样式表
- 具有较高值
priority
属性的模板具有较高的优先级
- 没有
priority
属性的模板被分配默认优先级。具有更多特定模式的模板优先。
- 如果前面的三个步骤考虑了多个模板,但是XSLT处理器可以通过默认文件中的最后一个来恢复,那么这是一个错误。
在您的具体情况下,两个模板具有相同的优先级,因此上述#4适用。为了证明:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match=
"vehicle_details[preceding-sibling::vehicle_type = '4x4']/*">
template1
</xsl:template>
<xsl:template match="vehicle_details[descendant::color = 'red']/*">
template2
</xsl:template>
</xsl:stylesheet>
应用于此输入(两个模板匹配):
<root>
<vehicle_type>4x4</vehicle_type>
<vehicle_details>
<color>red</color>
</vehicle_details>
</root>
输出:
template2
但是,如果我们换模板的顺序:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="vehicle_details[descendant::color = 'red']/*">
template2
</xsl:template>
<xsl:template match=
"vehicle_details[preceding-sibling::vehicle_type = '4x4']/*">
template1
</xsl:template>
</xsl:stylesheet>
然后输出是:
template1
一个很好的解释必须绝对清楚地表明导入优先级和'priority'是两个不同的东西,不管在导入的样式表的模板都有,它的优先级是优先级有多高,比任何模板的优先级低导入样式表。 – 2011-04-19 02:35:58
@Dimitre - 我打算按顺序阅读规则。也许这并不明确。我已经添加了一些解释。 – 2011-04-19 02:44:20
+1正确答案。 **观察:依赖错误恢复机制是不好的做法** – 2011-04-19 15:55:06