0

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?

4

1 回答 1

1

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:

  • doc("test.xml") is a function invocation: it retrieves a document at the given URI (http://www.xqueryfunctions.com/xq/fn_doc.html)
  • /data is an XPath step; it selects all the <data/> elements that are immediate children of the context node (in this case, the document)
  • /person is 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 predicate

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

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

  • the brackets signify the beginning and end of the predicate
  • last selects 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 to true if any atom in the left-hand sequence is present in the right-hand sequence
  • $last is a variable bound at the beginning of your XQuery expression (let $last := "Proj")

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".

于 2016-02-24T20:10:12.437 回答