I use zorba as XQuery processor. I am trying to figure out how to return more than one elements with XQuery. Then I found this.
xml doc:
<data>
<person>
<name>Me</name>
<last>Proj</last>
<mrn>54</mrn>
<phone>43434</phone>
<fax>34343</fax>
</person>
<person>
<name>Me2</name>
<last>Proj</last>
<mrn>542</mrn>
<phone>434342</phone>
<fax>343432</fax>
</person>
</data>
xqy doc:
let $last :="Proj"
return
<html>
<head><title>Example</title></head>
<body>
<table frame="box">
<tr>
<td>Name</td>
<td>Last</td>
<td>MRN</td>
</tr>
{
for $person in ( doc("test.xml")/data/person\[last=$last\] )
return
<tr>
<td>{$person/name/string()}</td>
<td>{$person/last/string()}</td>
<td>{$person/mrn/string()}</td>
</tr>
}
</table>
</body>
</html>
command line:
$ zorba -i -f -q test.xqy
test.xqy>:13,45: static error [err:XPST0003]: invalid expression: syntax error: unexpected character "\"; raised at /tmp/zorba20151126-72875-uij83f/zorba-3.0/src/compiler/api/compiler_api.cpp:252
I am pretty new to XQuery. Could someone point out how to make this work?
Zorba is complaining about this expression in your
forclause:The backslashes are not valid XQuery or XPath; removing them will leave you a valid expression:
Update: here's a breakdown of the expression:
doc("test.xml")is a function invocation: it retrieves a document at the given URI (http://www.xqueryfunctions.com/xq/fn_doc.html)/datais an XPath step; it selects all the<data/>elements that are immediate children of the context node (in this case, the document)/personis another XPath step; it selects all the<person/>elements that are immediate children of the context node (in this case, each of the previously selected<data/>elements)[last=$last]is an XPath predicate; it filters the context node (in this case, each of the previously selected<person/>elements) by testing if the node matches the predicateTo further deconstruct the XPath steps
/dataand/person:/is shorthand for thechild::axis; the expanded form of/datais/child::data//, which expands todescendant-or-self::To further deconstruct the predicate
[last=$last]:lastselects the<last/>elements that are immediate children of the context node (in this case, the previously selected<person/>elements)=is the set equivalence operator; it evaluates totrueif any atom in the left-hand sequence is present in the right-hand sequence$lastis a variable bound at the beginning of your XQuery expression (let $last := "Proj")In summary, the predicate
[last=$last]will evaluate totruefor any<person/>element that contains a<last/>element with a string value equal to"Proj".