xmlxqueryzorba

return more than one elements with XQuery:invalid expression


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?


Solution

  • Zorba is complaining about this expression in your for clause:

    doc("test.xml")/data/person\[last=$last\]
    

    The backslashes are not valid XQuery or XPath; removing them will leave you a valid expression:

    doc("test.xml")/data/person[last=$last]
    

    Update: here's a breakdown of the expression:

    To further deconstruct the XPath steps /data and /person:

    To further deconstruct the predicate [last=$last]:

    In summary, the predicate [last=$last] will evaluate to true for any <person/> element that contains a <last/> element with a string value equal to "Proj".