在BaseX 8.2中使用元素节点测试的XQuery文档节点测试会在根元素之前出现注释。为什么?

时间:2022-05-15 15:42:23

In BaseX 8.2, I'm trying to assign to an XQuery variable, a document node whose root element has a specific name. The source XML looks like this:

在BaseX 8.2中,我试图分配给一个XQuery变量,一个文档节点,其根元素具有特定的名称。源XML看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<!--A comment-->
<myRootElement/>

To get the document node, I type-check it using a DocumentTest:

要获取文档节点,我使用DocumentTest进行类型检查:

declare variable $docnode as document-node(element(myRootElement)) := doc("pathToSourceFile");

However, I get the following error message: XPTY0004: Cannot treat document-node() as document-node(document-node()(myRootElement))...

但是,我收到以下错误消息:XPTY0004:无法将document-node()视为文档节点(document-node()(myRootElement))...

This is quite unexpected because the assignment succeeds if there's no comment before the root element <myRootElement>. This means that the presence of the comment makes the query to fail.

这是非常意外的,因为如果根元素 之前没有注释,则赋值成功。这意味着注释的存在使查询失败。

However, that is not the expected behavior, unless XQuery behaves differently than XSLT in this respect (please let me know if this is the case). In XSLT, according to Michael Kay (p.671 ¶6 XSLT 2.0 and XPath 2.0 4th Ed.) the DocumentTest checks the following:

但是,这不是预期的行为,除非XQuery在这方面的行为与XSLT不同(如果是这种情况,请告诉我)。根据Michael Kay(p.671¶6XSLT 2.0和XPath 2.0 4th Ed。),在XSLT中,DocumentTest检查以下内容:

The document node must be the root of a tree that corresponds to a well-formed XML document. Specifically this means that the document node must have exactly one element node, and no text nodes, among its children. It is allowed to have comments and processing instructions before or after the element node.

文档节点必须是对应于格式良好的XML文档的树的根。具体而言,这意味着文档节点必须在其子节点中只有一个元素节点,并且没有文本节点。允许在元素节点之前或之后具有注释和处理指令。

In fact, the following transformation on Saxon, with the same input XML works well:

事实上,对Saxon的以下转换,使用相同的输入XML效果很好:

<xsl:transform version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="xml" encoding="utf-8"/>

  <xsl:template match="/">
    <xsl:variable name="docnode" as="document-node(element(myRootElement))" select="."/>
    <xsl:value-of select="$docnode/*/name(.)"/>
  </xsl:template>

</xsl:transform>

The assignment to variable docnode succeeds, and the output is:

对变量docnode的赋值成功,输出为:

<?xml version="1.0" encoding="utf-8"?>myRootElement

So, why does a DocumentTest with an ElementTest on an XML document with a comment before the root element works in Saxon, but not in BaseX? Maybe, there's something new for me to learn about XQuery.

那么,为什么带有ElementTest的DocumentTest在带有注释的XML文档之前在Saxon中工作,而不是在BaseX中?也许,我有一些新的东西要学习XQuery。

1 个解决方案

#1


1  

As indicated in the comments, you got everything right. The bug has been resolved in the latest 8.2.1 snapshot. Thanks for reporting this.

如评论中所示,你把一切都搞定了。该错误已在最新的8.2.1快照中得到解决。谢谢你报道这个。

#1


1  

As indicated in the comments, you got everything right. The bug has been resolved in the latest 8.2.1 snapshot. Thanks for reporting this.

如评论中所示,你把一切都搞定了。该错误已在最新的8.2.1快照中得到解决。谢谢你报道这个。