Hi Christian,
thank you for the deep explanation. I've just got to use XQuery recently; my
knowledge is primarily based on XPath 1.0 acquired 15y ago. I understand the
underlying string value comparison of nodes/node sets. Even though performance
was not my goal, thank you for the suggestion of 'is operator'.
However, I still don't understand, why the -w parameter should do the trick. I
would expect the parsing is done once on the document load and $rootElement
variable is just holding a document's node. Then string value of either
$rootElement and parent::* should be identical for identical nodes. Do I miss
something?
Thank you,
Jan
-Original Message-
From: Christian Grün
Sent: Wednesday, May 26, 2021 11:41
To: ydy...@post.cz
Cc: BaseX
Subject: Re: [basex-talk] BUG node comparison
Hi Jan,
Thanks for your feedback to the mailing list.
Your query will return the expected paths if you call BaseX as follows:
basex.bat -w -i datafile.xml queryfile.xq
A short explanation: By default, BaseX ignores whitespaces when parsing XML
documents. By specifying -w, whitespace chopping can be suppresed [1,2]. If you
want to permanently disable whitespace chopping, you can achieve that by adding
"CHOP=false" option in your .basex configuration file [2]
A more comprehensive explanation (just ignore it if you are aware of all the
details): In your query, you are checking if the string value of a parent
element is identical to the string value of $rootElement.
The following comparison is equivalent to yours:
//Section[parent::*/data() != $rootElement/data()]
This is the reason why it matters if the whitespace-only nodes will be parsed
or not. – If your actual objective is to compare the XML structure, you could
the following:
//Section[not(deep-equal(parent::*, $rootElement))]
The following solution compares node identities:
//Section[not(parent::* is $rootElement)]
Hope this helps
Christian
[1] https://docs.basex.org/wiki/Command-Line_Options
[2] https://docs.basex.org/wiki/Configuration
> Using BaseX 9.5.1, cmd-line “basex.bat -i datafile.xml queryfile.xq”
> leads to no result at all. I’m convinced there should be result
> equivalent to the one of the online xquery tester
> (https://www.videlibri.de/cgi-bin/xidelcgi)
>
> For a modified query using string comparison based on fn:path(), BaseX gives
> the expected result.
>
>
>
> %%% query begin %%%
>
> let $rootElement := /child::*
>
> for $x in //Section[parent::* != $rootElement]
>
> return fn:path($x)
>
> %%% query end %%%
>
>
>
> %%% data begin %%%
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> %%% data end %%%
>
>
>
> %%% modified query begin %%%
>
> let $rootElementPath := fn:path(/child::*)
>
> for $x in //Section[fn:path(parent::*) != $rootElementPath]
>
> return fn:path($x)
>
> %%% query end %%%
>
>
>
> Kind regards
>
> Jan Červák