Sunday, May 6, 2007

Get a Document's Properties by Attribute Value from MarkLogic Server

Here's a query to get a document's properties from MarkLogic Server using an attribute value. The attribute name is "id" and it is on the node named "document." I used the cts:element-attribute-value-query() function because I can set the case sensitivity and other options. The entire <prop:properties> node is returned.

define variable $myId as xs:string external
(:let $myId := 'ID1234':)

for $i in cts:search(//document,
cts:element-attribute-value-query(xs:QName("document"),
xs:QName("id"),
$myId,
"case-insensitive"
)
)
return $i/property::node()/..

You can pass in a variable from an external app or you can define it in the query using let. You can also tack on some additional metadata, like the URI of the document and any collections it belongs to. Change the return block for this to something like:

return
<result>
{ $i/property::node()/.. }
<uri> { base-uri($i) } </uri>
<collections> { xdmp:document-get-collections(base-uri($i)) } </collections>
</result>

No comments: