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.





Thanks so much for this. I was able to come up with a solution based on your answer!
– user2725782
Aug 22 at 20:27



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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế