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.

XML:

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

But it should not consider <name> element under <bb>

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.

  1. Using name space - define different namespace for <aa> and <bb> element and use cts:element-value-query() method.
  2. In cts:search define the first parameter as fn:doc()//aa

1 answer

  • answered 2017-01-11 14:17 BenW

    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")