How to return all elements in an element range index in Marklogic
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.
@id
<series/>
@id
cts:element-values()
The end result I'm looking for should be like this:
iot Institute of Theology
2 Answers
2
You can get tuples of index values using cts:value-tuples
.
cts:value-tuples
For example, this will return pairs of series/@id
and series
:
series/@id
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.
<series>
<series>
series/@id
series
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.
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.
<series>
cts:value-tuples()
@id
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)
Good solution! Just note that one or more positional indexes may be necessary for that to always be accurate.
– wst
Aug 23 at 16:14
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks so much for this. I was able to come up with a solution based on your answer!
– user2725782
Aug 22 at 20:27