xml - BaseX 8.2 中带有元素节点测试的 XQuery 文档节点测试会在根元素之前出现注释时抛出异常。为什么?

标签 xml xslt xquery saxon basex

在 BaseX 8.2 中,我尝试将其分配给 XQuery 变量,这是一个根元素具有特定名称的文档节点。源 XML 如下所示:

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

为了获取文档节点,我使用 DocumentTest 对其进行类型检查:

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

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

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

但是,这不是预期的行为,除非 XQuery 在这方面的行为不同于 XSLT(如果是这种情况,请告诉我)。在 XSLT 中,根据 Michael Kay(第 671 页¶6 XSLT 2.0 和 XPath 2.0 第 4 版)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.

事实上,在 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>

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

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

那么,为什么在根元素之前带有注释的 XML 文档上使用 DocumentTestElementTest 在 Saxon 中有效,但在 BaseX 中无效?也许,关于 XQuery,我有一些新的东西需要学习。

最佳答案

如评论中所述,您做对了一切。该错误已在 latest 8.2.1 snapshot 中得到解决.感谢您报告此事。

关于xml - BaseX 8.2 中带有元素节点测试的 XQuery 文档节点测试会在根元素之前出现注释时抛出异常。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30686201/

相关文章:

java - 在滑动选项卡布局中停留在同一选项卡中时切换到不同的 fragment

xml - XSLT:如何检查某些节点在一个父节点中是否全部相等

java - 我们如何在没有任何编码的情况下渲染重音字符

xml - 在 XQuery 中声明默认元素命名空间时抛出 "Expecting expression"异常

xml - XQuery - 分组和计数

java - settings.xml 中的 Maven 命令行参数

xml - WebLogic XML 外部实体解析 : Is there another way besides using the XML Registry?

xml - XSLT 中对列表的值替换

xml - xsl :sort an XML file using multiple elements

xml - 有没有办法使用 XPath 或 XQuery 在 SoapUI 中测试无序 XML 标记中的值?