简体   繁体   中英

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. The source XML looks like this:

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

To get the document node, I type-check it using a 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))...

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:

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.

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

<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:

<?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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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