Issue with selecting child nodes
Wednesday, 13 June 2018 · 2 messages
SF
Shaun Flynn13 Jun 2018 20:53 #
Hello there,
I have the following query:
declare variable $queryPath external := "D:\NROD
Projects\xqCrews\diagrams.v3\";
declare variable $testSet := doc($queryPath || "tests\testSet1.xml");
$testSetwhere testSet1.xml is:
<test>
<test1>
<content>Foobar</content>
</test1>
<test1>
<content>Foobar</content>
</test1>
</test>When I run $testSet/*, I would expect to get the following result:
<test1>
<content>Foobar</content>
</test1>
<test1>
<content>Foobar</content>
</test1>However, I get the complete document in testSet1.xml
When I run this against testSet1.xml
$testSet/*/*then I get what would I expect to see:
<test1>
<content>Foobar</content>
</test1>
<test1>
<content>Foobar</content>
</test1>Am I misunderstanding something?
Shaun
LW
Leonard Wörteler13 Jun 2018 21:07 #
Hi Shaun,
Quoted text · Shaun Flynn · 2 lines
On 13.06.2018 20:53, Shaun Flynn wrote: > declare variable $testSet := doc($queryPath || "tests\testSet1.xml");
here you bind a whole XML document (and therefore its corresponding, invisible *document node*) to
$testSet. This is a wrapper around the root element that associates the XML content with the URI of its surrounding document.Quoted text · Shaun Flynn · 10 lines
> When I run $testSet/*, I would expect to get the following result: > > <test1> > <content>Foobar</content> > </test1> > <test1> > <content>Foobar</content> > </test1> > > However, I get the complete document in testSet1.xml
This is because the root element
test is the first and only child of the document node.Quoted text · Shaun Flynn · 5 lines
> When I run this against testSet1.xml > > $testSet/*/* > > then I get what would I expect to see: [...]
This is correct (if confusing). `$testSet/*` is the first child of the
document node, the `test` element, so `$testSet/*/*` is the set of
`test`'s children.Hope that helps,
Leo Wörteler
[1] https://www.w3.org/XML/Datamodel.html#document
Written for BaseX 9.0. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery