basex - How to find the lowest common ancestor of two nodes in XQuery? -
suppose input xml
<root> <entry> <title>test</title> <author>me</author> </entry> </root>
i find lowest common ancestor of title
, author
. tried following code in basex:
let $p := doc('t.xq')//title, $q := doc('t.xq')//author, $cla := ($p/ancestor-or-self::node() intersect $q/ancestor-or-self::node()) return $cla
but returns nothing (blank output).
your code works totally fine me, apart returning all common ancestors.
the last common ancestor
since they're returned in document order , last common ancestor must last node, extend [last()]
predicate.
declare context item := document { <root> <entry> <title>test</title> <author>me</author> </entry> </root> }; let $p := //title, $q := //author, $cla := ($p/ancestor-or-self::node() intersect $q/ancestor-or-self::node())[last()] return $cla
files , databases
if query posted not return anything, might working on file t.xq
. intersect
requires nodes compared in same database, each invocation of doc(...)
on file creates new in-memory database. either create database in basex contents, or like
declare variable $doc := doc('t.xq');
and replace subsequent doc(...)
calls $doc
(which references single in-memory database created file).
Comments
Post a Comment