无法在 XSLT 2.0 的匹配 XPath 表达式中使用模板参数? [英] Not possible to use template parameter in the match XPath expression in XSLT 2.0?
问题描述
根据这个所以问题应该可以在 match
的 XPath 表达式中使用参数.但是,如果要在同一模板中使用 xsl:template
的 xsl:param
,它似乎不起作用.
According to this SO question it should be possible to use parameters in the XPath expression for match
. However it does not seem to work if xsl:param
of a xsl:template
is to be used in the same template.
我的 XML 文件如下所示
My XML file looks as follows
<?xml version="1.0" encoding="UTF-8"?>
<myRoot>
<myNode myAttribute="3">
<myChildAttribute myChildAttribute="a" />
</myNode>
<myNode myAttribute="2">
<myChildAttribute myChildAttribute="b" />
</myNode>
<myNode myAttribute="1" />
</myRoot>
和我的 XSL 文件一样.
and my XSL file like that.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="myRoot">
<xsl:apply-templates select="myNode">
<xsl:sort select="@myAttribute" />
<xsl:with-param name="myParam" select="max(myNode/@myAttribute)" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="myNode[node() and @myAttribute = $myParam]">
<xsl:param name="myParam" />
<xsl:for-each select="myChildAttribute">
INSERT INTO a(b) VALUES ('<xsl:value-of select="@myChildAttribute" />');
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
不幸的是,当使用 SAXON 9HE 运行时,它以以下错误结束
Unfortunately when run with SAXON 9HE it ends with the following error
XPST0008: Variable myParam has not been declared (or its declaration is not in scope)
在同一个模板的match-XPath表达式中不能使用模板的参数吗!?
Is it not possible to use the parameter of a template in the match-XPath expression of the same template!?
推荐答案
是不是不能在模板中使用参数match-XPath 相同模板的表达式!?
Is it not possible to use the parameter of a template in the match-XPath expression of the same template!?
否,选择模板执行时,模板匹配表达式中的任何变量/参数都必须在范围内(已定义/可见).
No, any variable/parameter in the match expression of the template must be in scope (defined/visible) when templates are selected for execution.
因为模板是 XSLT 指令(在全局级别定义),所以在范围内(他们可以看到)的唯一变量/参数是全局级别的变量/参数.
Because templates are XSLT-directives (defined at the global level), the only variables/parameters that are in scope (that they can see) are global - level variables/parameters.
模板的参数只有在模板被选择执行之后才会被传递给它——而不是在此之前.这意味着在执行模板选择过程时,该参数的值不存在.
A parameter of a template is passed to it only after the template is selected for execution -- not before that. This means that the value of this parameter doesn't exist when the template selection process is being executed.
因此,如果要在执行过程的模板选择中使用非全局表达式,则需要在相应xsl:apply-templates的
指令,这里表达式可以被计算——不在模板的 select
属性中提供match
属性中,这里表达式不能被计算.
Thus, if a non-global expression is to be used in the template selection for execution process, it needs to be supplied in the select
attribute of the corresponding xsl:apply-templates
instruction, where this expression can be evaluated -- not in the match
attribute of the template, where this expression cannot be evaluated.
为了说明这一点,下面的代码更正了所提供代码中的问题:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="myRoot">
<xsl:apply-templates select="myNode[@myAttribute = max(../myNode/@myAttribute)]">
<xsl:sort select="@myAttribute" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="myNode[node()]">
<xsl:for-each select="myChildAttribute">
INSERT INTO a(b) VALUES ('<xsl:value-of select="@myChildAttribute" />');
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<myRoot>
<myNode myAttribute="3">
<myChildAttribute myChildAttribute="a" />
</myNode>
<myNode myAttribute="2">
<myChildAttribute myChildAttribute="b" />
</myNode>
<myNode myAttribute="1" />
</myRoot>
没有产生错误,这是转换的输出(我不能说正确的输出",因为没有定义要求,因此无法验证它们.我对此持保留意见代码:例如使用 <xsl:sort>
子元素 xsl:apply-templates
是没有意义的,因为它将对相等的 ( max() ) 值进行排序并且对相等值的序列进行排序会产生相同的序列):
no error is produced and this is the output of the transformation (I cannot say "the correct output" because no requirements have been defined thus they cannot be verified. And I have my reservations about this code: for example the use of the <xsl:sort>
child of xsl:apply-templates
is not meaningful, because it will sort equal ( max() ) values and sorting of a sequence of equal values produces the same sequence):
INSERT INTO a(b) VALUES ('a');
这篇关于无法在 XSLT 2.0 的匹配 XPath 表达式中使用模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!