Dixit Singla
Dixit Singla

Reputation: 2620

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

Upvotes: 0

Views: 283

Answers (2)

You are not clear if you just want to return the value of the element or just use it to refine your search criteria.

Another option is to use a field and define the path there and then use field-value-query or field-word-query. And if you desired even more performance, you can still add a range index to this solution after-the-fact.

Upvotes: 0

BenW
BenW

Reputation: 433

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

Upvotes: 2

Related Questions