Search code examples
marklogicmarklogic-10

cts:uris with namespace binding


I have multiple XML documents stored in MarkLogic, my requirement is to get the URIs of the document when there is a specific element in it, all XMLs are stored with namespace in it.

sample XML from MarkLogic

<item xmlns="http://namespaces.test.org/namespaces/ab">
    <itemId>TestItem</itemId>
    <languageCode>EN</languageCode>
</item>

following query was executed with no result,

cts:uris((), (), cts:and-query(
  cts:element-query(xs:QName("itemId"), cts:true-query()))
)

looks like namespace is causing issue here, I am not sure how to specify the namespace in cts:uris()

Any help is appreciated.


Solution

  • Your itemId element is in the http://namespaces.test.org/namespaces/ab namespace. The parent element item declares the namespace and doesn't use a namespace-prefix, so it's a little harder to see that at first.

    Your element-query is looking for an itemId that isn't bound to any namespace, so it doesn't find anything.

    In order to query for docs that have that itemId element, you need to construct a QName that includes the namespace:

    declare namespace ab = "http://namespaces.test.org/namespaces/ab";
    
    cts:uris((), (), cts:and-query(
      cts:element-query(xs:QName("ab:itemId"), cts:true-query()))
    )