简体   繁体   English

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

[英]XQuery document node test with an element node test in BaseX 8.2 throws in the presence of comments before the root element. Why?

In BaseX 8.2, I'm trying to assign to an XQuery variable, a document node whose root element has a specific name. 在BaseX 8.2中,我试图分配给一个XQuery变量,一个文档节点,其根元素具有特定的名称。 The source XML looks like this: 源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>. 这是非常意外的,因为如果根元素<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). 但是,这不是预期的行为,除非XQuery在这方面的行为与XSLT不同(如果是这种情况,请告诉我)。 In XSLT, according to Michael Kay (p.671 ¶6 XSLT 2.0 and XPath 2.0 4th Ed.) the DocumentTest checks the following: 根据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. 文档节点必须是对应于格式良好的XML文档的树的根。 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. 允许在元素节点之前或之后具有注释和处理指令。

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? 那么,为什么带有ElementTestDocumentTest在带有注释的XML文档之前在Saxon中工作,而不是在BaseX中工作? Maybe, there's something new for me to learn about XQuery. 也许,对于我来说,了解XQuery有一些新的东西。

As indicated in the comments, you got everything right. 如评论中所示,你把一切都搞定了。 The bug has been resolved in the latest 8.2.1 snapshot . 该错误已在最新的8.2.1快照中得到解决。 Thanks for reporting this. 感谢您报告此事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM