2

有人可以解释一下为什么以下给我的错误: Keyword xsl:template may not contain xsl:next-match

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"  version = "3.0">

<xsl:template match="*">
  <xsl:value-of select="name(.)"/><br/>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="rc2">
  <h1>this is first match</h1>
  <xsl:next-match/>
</xsl:template>

</xsl:stylesheet>

虽然这个版本没有错误,但当然它只匹配一个

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"  version = "3.0">

<xsl:template match="*">
  <xsl:value-of select="name(.)"/><br/>
  <xsl:apply-templates/>
  <xsl:next-match/>
</xsl:template>

<xsl:template match="rc2">
  <h1>this is first match</h1>
</xsl:template>

</xsl:stylesheet>

我的测试 xml 文件是:

<?xml version="1.0"?>
<rc2/>

(问题修订编辑)我正在使用 Msxml2.XSLTemplate.6.0、Msxml2.FreeThreadedDOMDocument.6.0 和 Msxml2.DOMDocument.6.0

4

1 回答 1

4

您使用的是什么 XSLT 处理器?xsl:next-match 需要 XSLT 2.0,我猜您使用的是 XSLT 1.0 处理器。

您在 xsl:stylesheet 标头中说过 version="3.0",这使事情变得复杂。如果样式表显示 version="3.0" 并且您使用 XSLT 1.0 处理器运行它,那么它将以“向前兼容模式”运行。在这种模式下,XSLT 1.0 中不可用的 XSLT 指令只有在实际执行时才会导致错误。这个想法是允许您运行样式表,您可以在其中动态决定执行哪些代码模板,方法是询问处理器它支持什么,例如使用 system-property() 或 element-available() 函数。

于 2014-03-28T15:06:22.330 回答