Stephane,
You are on the right track. To search the entire document, use the
xdmp:directory() expression as the "searchable expression" but you will get
entire documents back from that search.
To find the author you want you may use cts:contains(). This is the function
that performs the "filtering" check referenced in our documentation, where it
checks an in-memory XML structure to see if a query condition holds rather than
checking the DB indexes for the query condition. This example (namespace
removed for convenience) should help:
xquery version "1.0-ml";
let $d :=
<doc docId="1">
<title>foobar</title>
<authors>
<author>
<firstname>stephane</firstname>
<lastname>toussaint</lastname>
</author>
<author>
<firstname>my</firstname>
<lastname>self</lastname>
</author>
</authors>
</doc>
return xdmp:document-insert("/test/text.xml", $d)
;
let $q := cts:word-query("stephane") (: "stephane" is a shorthand for this
word query :)
return cts:search(/doc, $q)//author[cts:contains(., $q)]
For more precision, you may also use cts:element-query(xs:QName("author"),
cts:word-query("stephane")).
Yours,
Damon
From: [email protected]
[mailto:[email protected]] On Behalf Of Stephane Toussaint
Sent: Tuesday, May 24, 2011 8:28 AM
To: General MarkLogic Developer Discussion
Subject: [MarkLogic Dev General] How to specified a result root node ?
Hi,
I'm trying to specified a result root node for my query in a customizable
context.
Take this documents for instance ("/test/doc1.xml") :
<?xml version="1.0" encoding="UTF-8"?>
<doc docId="1" xmlns="sto:test">
<title>foobar</title>
<authors>
<author>
<firstname>stephane</firstname>
<lastname>toussaint</lastname>
</author>
<author>
<firstname>my</firstname>
<lastname>self</lastname>
</author>
</authors>
</doc>
I'd like to retrieve for instance doc which contains title "foobar". Easy
enough the query will be :
cts:search(
xdmp:directory("/test/"),
cts:element-value-query(fn:QName("sto:test", "title"), "foobar")
)
Now if I want to retrieve docs written by toussaint.
cts:search(
xdmp:directory("/test/"),
cts:element-value-query(fn:QName("sto:test", "lastname"), "toussaint")
)
Ok, so far so good. Now I want to change the root of the element to return. I'd
like to return authors instead of the doc directly.
Retrieving the authors of a particular doc is easy :
cts:search(
xdmp:directory("/test/"),
cts:element-value-query(fn:QName("sto:test", "title"), "foobar")
)//*:author
But now if I want only authors with 'toussaint' has lastname
cts:search(
xdmp:directory("/test/"),
cts:element-value-query(fn:QName("sto:test", "
lastname
"), "toussaint")
)//*:author
This query doesn't work because it return every authors for the document with
an author named toussaint (return 2 results instead of one).
The thing is to change the root node of the search query :
cts:search(
xdmp:directory("/test/")//*:author,
cts:element-value-query(fn:QName("sto:test", "lastname"), "toussaint")
)
But how could I combined for instance title with lastname and return only the
author ?
cts:search(
xdmp:directory("/test/")//*:author,
cts:and-query((
cts:element-value-query(fn:QName("sto:test", "title"), "foobar"),
cts:element-value-query(fn:QName("sto:test", "lastname"), "toussaint")
))
)
This return nothing because there is no 'title' element under 'author'
cts:search(
xdmp:directory("/test/"),
cts:and-query((
cts:element-value-query(fn:QName("sto:test", "title"), "foobar"),
cts:element-value-query(fn:QName("sto:test", "lastname"), "toussaint")
))
)//*:author
This returns 2 authors instead of only the matching one.
So briefly the question is, whatever the constraint part of my query I want to
be able to retrieve the closet element of the root element I choose to return
(I hope I'm clear enough).
Is there something that can be done with the cts api ?
Thanks,
Stéphane
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general