结果树碎片背后的基本原理是什么?

What's the rationale behind result tree fragments?

XSLT 1.0 在 XPath 1.0 提供的数据类型的基础上增加了一种额外的数据类型:结果树片段。

This additional data type is called result tree fragment. A variable may be bound to a result tree fragment instead of one of the four basic XPath data-types (string, number, boolean, node-set). A result tree fragment represents a fragment of the result tree. A result tree fragment is treated equivalently to a node-set that contains just a single root node. However, the operations permitted on a result tree fragment are a subset of those permitted on a node-set. An operation is permitted on a result tree fragment only if that operation would be permitted on a string (the operation on the string may involve first converting the string to a number or boolean). In particular, it is not permitted to use the /, //, and [] operators on result tree fragments.
https://www.w3.org/TR/xslt-10/#section-Result-Tree-Fragments

对我来说,这似乎毫无意义。我无法 理解为什么会有人想要这样做!结果树片段看起来就像节点集的垃圾版本,需要 and a language extension 允许程序员解决这个看似任意的限制。

为了进一步解决结果树片段的无用问题,这里是我 窃取 放在一起以在 MSXSL 中复制 exsl:node-set 的兼容性垫片:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exsl="http://exslt.org/common"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="exsl msxsl">
  <!-- exsl:node-set -->
  <msxsl:script language="JScript" implements-prefix="exsl"><![CDATA[
    this['node-set'] = function (x) {
      return x;
    }
  ]]></msxsl:script>
</xsl:stylesheet>

这个字面上只是returns结果树片段未改变,表明MSXSL甚至不关心实现结果树片段作为一种不同的类型,只是将其视为与 node-set 相同,进一步表明它首先没有真正的意义!

为什么会存在结果树碎片?

我当时不在工作组,但以下交流可能会有所启发。 2001 年 4 月,在 XSLT 1.1 的开发过程中,我向工作组询问:

Can any one try to explain to me why there is a perceived problem with the "result tree fragment as node-set" facility as defined in the XSLT 1.1 WD? I keep hearing that it won't work with the XPath 2.0 type system, but I can't see why.

I can see us wanting to change it so that the data type is "node" rather than "node-set", but apart from that, I fail to see what the problem is.

Is it perhaps that someone has in mind doing away with the root node of the temporary tree, and making the value of the variable instead be the sequence of nodes that are currently modelled as children of this root? If so, why would that change be useful?

詹姆斯·克拉克回复:

Is it perhaps that someone has in mind doing away with the root node of the temporary tree, and making the value of the variable instead be the sequence of nodes that are currently modelled as children of this root?

是的。

If so, why would that change be useful?

(a) 因此指令可以 return 个节点而无需复制它们。

(b) 这样你就可以使用指令来 return 除了 节点。

解释更多的东西需要我解释我是如何 希望看到 XPath、XSLT 和 XQuery 能够完美结合。在这 点,我只想说我认为我们需要协调元素 XSLT 和 XQuery 中的构造。这自然会导致他们 表达式和指令之间的鸿沟要小得多。我 认为它会变得同样尴尬和不合适 xsl:variable 自动复制并包装在根节点中的值 通过实例化它的内容来产生,因为它会这样做 通过评估在指定的表达式产生的值 select 属性。

我认为工作组发明了“结果树片段”的概念,因为他们希望为未来保留选择余地。他们知道语言将如何发展,他们认为 xsl:variable 创建一个具有完整导航功能的成熟节点会限制未来的选择。

回想起来我确信这是一个错误,因为它实际上并没有实现 objective。当我们在 2.0 中废除 RTF 时,我们仍然发现,出于向后兼容的原因,有必要制定一个奇怪的规则,即如果没有“as”属性,xsl:variable 总是构造一个文档节点。

值得注意的是,工作组中的任何人都没有想到二十年后人们仍然在使用 XSLT 1.0。 1.0 花了大约两年的时间开发,工作组完全预计在两年内,它会被更新的版本完全取代。因此,如果他们为下一个版本保留选项,他们非常愿意对语言施加限制。