How to perform element query on a duplicate element in Marklogic?
Sorry if the grammar in the asked question is wrong.
Problem: Assume the below XML as sample XML for the problem.
<publishers> <aa> <name>aaaaa</name> <address>aaa addr</address> </aa> <bb> <name>bbbb</name> <address>bbb addr</address> </bb> </publishers>
I want to perform search under
<name> element having parent element as
But it should not consider
<name> element under
Is there any way in MarkLogic to do the same.
I know few ways(which are listed below) to do this, but I am looking for some other options.
- Using name space - define different namespace for
<bb>element and use
cts:searchdefine the first parameter as
Probably the easiest way is to use cts:element-query, which creates queries that respect the element hierarchy.
In your example, this query will search for whatever name you want in "name" elements that fall within "aa" elements, ignoring "name" elements elsewhere.
cts:element-query(xs:QName("aa"), cts:element-value-query(xs:QName("name"), "whatever") )
And of course cts:element-query calls can be nested to create more complicated tree constraints.
An alternative approach would be to use cts:path-range-query, although whether that is a good approach depends on what exactly you're trying to do. It would require a range index for each path to a aaa/name elements you want to include in the search.
cts:path-range-query("/publishers/aa/name", "=", "whatever")