这个问题更容易用例子来描述,而不是用文本来描述。
使用下面的XML
<?xml version="1.0" encoding="UTF-8"?>
<tests>
<test>1</test>
<test>2</test>
</tests>如果我运行以下XSLT3
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
expand-text="true"
version="3.0">
<xsl:output method="xml" />
<xsl:mode on-no-match="shallow-copy" />
<!--<xsl:mode name="test" on-no-match="shallow-copy"/>-->
<xsl:template match="/">
<mytests>
<xsl:apply-templates/>
<xsl:apply-templates mode="test"/>
</mytests>
</xsl:template>
<xsl:template match="tests" mode="test">
<modetest>
<xsl:apply-templates mode="#current"/>
</modetest>
</xsl:template>
</xsl:stylesheet>我在Saxon 9中得到了以下输出
<?xml version="1.0" encoding="UTF-8"?>
<mytests>
<tests>
<test>1</test>
<test>2</test>
</tests>
<modetest>
1
2
</modetest>
</mytests>您可以看到,当使用模式" test“时,我们不会得到输出的test元素,只会得到该元素的内容。模式为"test“的元素"test”没有模板。
我会猜到,如果没有匹配,on- no - match =“浅拷贝”应该是从没有name属性的xsl:mode开始的?我的猜测是,no命名的xsl:mode将应用于所有no匹配,即使一个模式是有效的(除非另一个xsl:mode被定义为与当前模式匹配的名称)。如果您取消注释xsl:mode name=“name=”on- no -match=“浅拷贝”,那么一切都会正常工作(所以不需要变通方法,谢谢),但这意味着在应用模板中有很多很多模式的XSLT中,我需要定义很多很多命名的xsl:mode,才能获得身份模板行为。
有没有人能指出我是否做错了什么,或者这是否符合w3C规范?
发布于 2020-01-15 00:55:38
“我的猜测是,no命名的xsl:mode将应用于所有no匹配,即使模式是有效的”
对不起,你的猜测可能是错的。可以这样想:有一种模式的名称是#unnamed,任何没有@mode属性的xsl:template、xsl:apply-templates或xsl:mode元素都缺省为mode="#unnamed"。(这只是一个近似值,当指定default-mode时,它会变得更复杂,但这是基本概念。)
无法“批量”定义许多模式的属性,每个模式需要一个xsl:mode声明。
发布于 2020-01-14 21:17:16
任何模式的默认内置处理都是text-only-copy,请参见https://www.w3.org/TR/xslt-30/#modes“如果使用了模式名称(例如,在xsl:template声明或xsl:apply-templates指令中),并且该模式的声明没有出现在样式表中,则该模式是用默认属性隐式声明的”和https://www.w3.org/TR/xslt-30/#built-in-rule
有六组内置模板规则可用。选择的集合是由
xsl:apply-templates指令选择的模式的属性。此属性是使用xsl:mode声明的on-no-match属性设置的,该属性接受六个值deep-copy、shallow-copy、deep-skip、shallow-skip、text-only-copy或fail中的一个,缺省值为text-only-copy。
XSLT 2或1中的内置缺省处理没有什么不同。
如果必须使用单独的xsl:mode声明来单独声明标识转换似乎太麻烦,那么当然也可以编写一个模板
<xsl:template match="@* | node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:apply-templates mode="#current"/>
</xsl:copy>
</xsl:template>https://stackoverflow.com/questions/59734101
复制相似问题