Stephen Lee
Stephen Lee

Reputation: 73

Marklogic querying using XML string

Hi all i am trying to get this to work using CTS queries and a user input XML string that conforms to a CTS query.

test.xqy:

let $query:=xdmp:unquote(xdmp:get-request-field('query'))
let $results:=cts:search(/,$query)
return
<r>
<q>
{$query}
</q>
{$results}
</r>

query will be

<cts:element-value-query>
  <cts:element>date</cts:element>
  <cts:text xml:lang="en">2013-11-18</cts:text>
</cts:element-value-query>

What i dont understand is how come there are always no results. Using replacing $query with

let $query:=cts:element-value-query(xs:QName("date"),"2013-11-18")

always works.

So i dont understand whats the difference between a custom xml string converted with xdmp:unquote is any different from a cts:element-value-query. I have printed both of them out and they are the same.

Upvotes: 0

Views: 431

Answers (3)

Stephen Lee
Stephen Lee

Reputation: 73

The main reason as Mike says is that xdmp:unquote will not give me a cts:query.

What is need is to actually use cts:query itself to convert element into cts:query so after

let $query:=xdmp:unquote(xdmp:get-request-field('query'))
let $cts_query:=cts:query($query)
let $results:=cts:search(/,$cts_query/*)

U can find in under the search developers guide, Function to Construct a cts:query From XML

Upvotes: 0

mblakele
mblakele

Reputation: 7842

http://docs.marklogic.com/cts:search takes a cts:item not an XML element. If xdmp:get-request-field('query') is expected to return serialized cts:query XML, try this:

`cts:query(xdmp:unquote(xdmp:get-request-field('query'))/*)`

The /* step is necessary because xdmp:unquote returns a document node.

Upvotes: 1

Mike Sokolov
Mike Sokolov

Reputation: 7054

unquoting the XML string will return an XML document, not a cts:query. You might expect to get a type clash in that case, but I think that cts:search can also accept a string as a query, in which case the string is interpreted as a cts:word-query. So I think what is happening is that the string value of your XML document ("date 2013-11-18", modulo white space) is being interpreted as a word-query, and not matching anything.

To do what you are trying to do, I think you may need to use xdmp:eval() - not unquote. However I wouldn't really recommend allowing executable code to be passed in as a query parameter. That just cries out for abuse.

Upvotes: 0

Related Questions