XQuery expressions

Build 1501 on 14/Nov/2017  This topic last edited on: 5/Aug/2014, at 16:53

Besides the XPath syntax used in examples so far, GNQuery supports also a very simple version of the XQuery syntax. For example, the XPath query returning all stories with a specific title:

gn4:story[gn4:title='My title']

can be written using XQuery syntax as:

for $s in gn4:story where $s/gn4:title='My title'

See http://www.w3schools.com/xquery/default.asp for a quick introduction to XQuery.

Compared with full-blown XQuery implementation GNQuery supports only the ‘for’, ‘where’, ‘order by’ (already seen above) and ‘return’ clauses, uses XPath 1.0 for its expressions instead than XPath 2.0 and does not implement tuples or any templating to return complex XML values.

The ‘return’ clause modifies queries to return only specific values – e.g.:

for $s in gn4:story where $s/gn4:title='My title' return $s/@name

returns the names of all the stories with the specified title. The same query can be written using an XPath as:

gn4:story[gn4:title='My title']/@name

Another example:

for $i in gn4:image return $i/@pixWidth*$i/@pixHeight

returns the pixel area of all images in the system. Note that this query can be written only using an XQuery expression and not with a single XPath.

Queries returning specific values cannot be used when fetching objects – e.g. by ‘srv4 export’ – but only internally by code expecting those values.

When writing XQuery expressions the ‘dummy variable’ declared in the ‘for’ clause can be omitted in the ‘where’, ‘order by’ and ‘return’ clauses – this is not the case in standard XQuery implementations. For example:

for $s in gn4:story where $s/gn4:title='My title' return $s/@name

can be written as

for $s in gn4:story where gn4:title='My title' return @name