user2725782
user2725782

Reputation: 139

How to return all elements in an element range index in Marklogic

I have the following element in my XML:

<series id="iot" type="main">Institute of Theology</series>

What I am trying to do is to get all series values in the database along with the matching @id value in the most performance conscious way possible. I have set up an element range index on <series/> and also an attribute range index on @id. I've tried using cts:element-values() which works great to get the series element values, but I can't figure out how to return both the element and matching id values.

The end result I'm looking for should be like this:

iot Institute of Theology 

Upvotes: 2

Views: 348

Answers (2)

user2725782
user2725782

Reputation: 139

With a few small changes to wst's answer I was able to solve this. The XML I'm working with does indeed have more than one <series> element per document, but I found a workaround by using the "proximity=N" option in cts:value-tuples(). Setting proximity to 0 returned the @id and the closest series title.

Here's the code:

for $tuple in 
  cts:value-tuples((
        cts:element-attribute-reference(xs:QName("ia:series"), xs:QName('id')),
        cts:element-reference(xs:QName("ia:series"))
      ), "proximity=0")
let $values := json:transform-from-json($tuple)
return 
  for $value in $values
  let $id := $value/jsonNS:item[1]
  let $title := $value/jsonNS:item[2]
  return fn:concat($id, " | ", $title)

Upvotes: 3

wst
wst

Reputation: 11771

You can get tuples of index values using cts:value-tuples.

For example, this will return pairs of series/@id and series:

cts:value-tuples((
  cts:element-attribute-reference(xs:QName('series'), xs:QName('id')),
  cts:element-reference(xs:QName('series'))
  ))

However, the pairs will only be accurate within <series> if there is only one <series> per document. Otherwise you will get all of the combinations of series/@id and series per document, regardless of whether they are from the same element.

If that is the case, then your options are to either change your documents (or use fragment roots, which is probably not a good idea), or use another method like Template Driven Extraction, where the pairs could be projected into a "stand-off" index, like rows, and then queried separate from the document context.

Upvotes: 5

Related Questions