“上下文"的含义关于调用模板的参数 [英] Meaning of "context" in regard to parameters of a calling template

查看:115
本文介绍了“上下文"的含义关于调用模板的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想认为我对XSLT有很好的了解,但以下几点使我难以理解: 为什么从 not 显式声明的调用模板无法访问xsl:param?换句话说,如果我从模板A调用模板B:

I like to think that I have a good grasp of XSLT, but the following eludes me: Why is an xsl:param not accessible from a called template that does not explicitly declare it? In other words, if I call template B from template A:

样式表1

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:template match="/" name="A"><!--This template is named "A" for convenience only.-->
      <xsl:param name="hello" select="hello "/>
      <xsl:param name="world" select="world!"/>
      <xsl:call-template name="B"/>
   </xsl:template>

   <xsl:template name="B">
      <xsl:value-of select="concat($hello,$world)"/>
   </xsl:template>

</xsl:stylesheet>

为什么模板B不会自动采用模板A的参数作为上下文的一部分?我的理由如下.

Why does template B not automatically adopt the parameters of template A as part of the context? My rationale is as follows.

显然,调用模板不会以任何方式影响上下文:

Apparently, calling a template does not affect the context in any way:

对选定的<xsl:template>进行评估时不会对上下文进行任何更改:它使用与调用模板 1 <相同的上下文项,上下文位置和上下文大小/p>

The selected <xsl:template> is evaluated with no change to the context: it uses the same context item, context position, and context size as the calling template1


现在,上下文"在XSLT中实际上意味着什么,或者更确切地说,参数是否被视为上下文的一部分?在构成上下文的事物中,有 2 :

  • 所有值声明(xsl:variablexsl:param)都在范围内,该范围在计算表达式时作为静态上下文的一部分
  • 范围内所有变量的值,作为动态上下文的一部分
  • all variable declarations (xsl:variable and xsl:param) that are in scope at the point where an expression is evaluated, as part of the static context
  • the values of all variables that are in scope, as part of the dynamic context

这使我相信以下样式表与我展示的第一个样式表相同:

This leads me to believe that the following stylesheet is equal to the first one I have shown:

样式表2

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:template match="/" name="A">
      <xsl:param name="hello" select="hello "/>
      <xsl:param name="world" select="world!"/>
      <xsl:value-of select="concat($hello,$world)"/>
   </xsl:template>

</xsl:stylesheet>

但是很明显,只有第二个是正确的,而第一个会产生两个有关缺少变量声明的错误.如果模板调用不更改上下文并且参数被视为上下文的一部分,为什么必须在调用的模板中显式声明参数?

But obvously, only the second one is correct, whereas the first one produces two errors about missing variable declarations. Why must parameters be explicitly declared in called templates if a template call does not change the context and parameters are considered part of the context?

为清楚起见:

  • 我知道如何用xsl:with-param和参数声明来修改第一个样式表,这不是我要的.
  • 我知道XSLT 2.0中的隧道参数-您不必解释它们或建议它们作为替代方法.我不要求在未声明参数的模板中使用参数的方法-我想知道为什么首先它们不可用.
  • I know how to amend the first stylesheet with xsl:with-param and parameter declarations - that is not what I am asking for.
  • I am aware of tunnel parameters in XSLT 2.0 - you do not have to explain them or suggest them as an alternative. I am not asking for a way to use parameters in a template where they are not declared - I am wondering why they are not available in the first place.

1我的重点.参见 XSLT 2.0程序员参考,Michael Kay,第273页.
2请参见规范的相关部分此处或参阅Michael Kay的 XSLT 2.0程序员参考,第84f页.

1 My emphasis. See XSLT 2.0 Programmer's Reference, Michael Kay, page 273.
2 See the relevant part of the specification here or refer to Michael Kay's XSLT 2.0 Programmer's Reference, page 84f.

推荐答案

很好的问题,问得好.

您期望的行为在具有动态范围的语言中是自然的.但是XSLT使用词法作用域作为变量,而不是动态作用域.

The behavior you expect would be natural in a language with dynamic scope. But XSLT uses lexical scope for variables, not dynamic scope.

您问现在,上下文"在XSLT中实际上意味着什么,或者更确切地说,参数是否被视为上下文的一部分?

简短的回答:是的,对于它范围内的表达式而言,该参数是静态上下文的一部分(而缺少的是关于样式表中其他位置的表达式的静态上下文的事实);它的值是其范围内表达式动态上下文的一部分.并且(关键)xsl:call-template指令确实影响表达式的计算上下文.

Short answer: Yes, the parameter is part of the static context, for the expressions in its scope (and its absence is a fact about the static context of expressions elsewhere in the stylesheet); its value is part of the dynamic context for expressions in its scope. And (crucially) the xsl:call-template instruction does affect the context in which expressions are evaluated.

更长的答案:具体内容可在 XSLT 2.0规范中找到.在第2.5 节中,规范告诉我们上下文分为两部分:静态上下文和动态上下文. 第5.4节提供了完整的详细信息; 第5.4.1节将范围内变量"列为静态上下文.

Longer answer: The specifics are to be found in the XSLT 2.0 spec. In section 2.5 the spec tells us that the context is divided into two parts: the static context and the dynamic context. Section 5.4 provides full details; section 5.4.1 lists "in-scope variables" as one component of the static context.

变量的范围规则在第9.7 节中给出.关键是:

The scope rules for variables are given in section 9.7. The key bit is this:

对于以下所有兄弟姐妹及其后代来说,局部变量绑定元素可见,但有两个例外:在被另一个变量绑定遮盖的任何区域中都不可见,并且在以xsl为根的子树中不可见:fallback指令,它是变量绑定元素的同级.

A local variable binding element is visible for all following siblings and their descendants, with two exceptions: it is not visible in any region where it is shadowed by another variable binding, and it is not visible within the subtree rooted at an xsl:fallback instruction that is a sibling of the variable binding element.

您遇到的明显矛盾主要取决于调用模板不会影响上下文"这一前提.尽管您拥有非常好的权限,但此前提实际上是不正确的.

The apparent contradiction you have run into relies crucially on the premise that "calling a template does not affect the context". This premise is not in fact correct, despite the fact that you have it on very good authority.

第10.1节中,规范规定指令不会改变焦点."这并不是说指令不会影响上下文.

In section 10.1, the spec says that "the xsl:call-template instruction does not change the focus." It does not say that the instruction leave the context unaffected.

在您引用迈克尔·凯(Michael Kay)的书的那段话中,我认为上下文"一词最好是上下文项"或动态上下文"的简称.即使是这样,句子也不是很正确:因为上下文的dynamic variables组件不同,所以被调用模板中的动态上下文与调用模板中的动态上下文并不完全相同.我认为您必须在这里减少MK的余地:所讨论的段落与他的书的XSLT 1.0版本基本上没有变化,并且在 context 的讨论中,XSLT 1.0中没有明确的表示形式,动态将变量和参数绑定到名称.但是,我认为可以肯定地说,您已经在MK的下一版中希望更改某些内容.

In the passage you quote from Michael Kay's book, I believe the term "context" is perhaps best taken as short for "context item", or perhaps "dynamic context". Even on that reading the sentence is not quite correct: because the dynamic variables component of the context differs, the dynamic context in the called template is not exactly the same as in the calling template. I think you have to cut MK some slack here: the paragraph in question is essentially unchanged from the XSLT 1.0 version of his book, and in XSLT 1.0 there is no explicit representation, in the discussion of context, of the dynamic binding of variables and parameters to names. But I think it's fair to say you've found something in the book MK might wish to change in the next revision.

这篇关于“上下文"的含义关于调用模板的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆