Using XQuery 1.0
This guide explains how to use XQuery 1.0 with XmlPrime.
This topic contains the following sections.
Loading the Input Document
In this example we will be using the bstore1.example.com/bib.xml document as described in XML Query Use Cases. The document is as follows:
<bib> <book year="1994"> <title>TCP/IP Illustrated</title> <author><last>Stevens</last><first>W.</first></author> <publisher>Addison-Wesley</publisher> <price>65.95</price> </book> <book year="1992"> <title>Advanced Programming in the Unix environment</title> <author><last>Stevens</last><first>W.</first></author> <publisher>Addison-Wesley</publisher> <price>65.95</price> </book> <book year="2000"> <title>Data on the Web</title> <author><last>Abiteboul</last><first>Serge</first></author> <author><last>Buneman</last><first>Peter</first></author> <author><last>Suciu</last><first>Dan</first></author> <publisher>Morgan Kaufmann Publishers</publisher> <price>39.95</price> </book> <book year="1999"> <title>The Economics of Technology and Content for Digital TV</title> <editor> <last>Gerbarg</last><first>Darcy</first> <affiliation>CITI</affiliation> </editor> <publisher>Kluwer Academic Publishers</publisher> <price>129.95</price> </book> </bib>
This document is assumed to be in the file bib.xml.
First, we create a new XmlNameTableXmlNameTableXmlNameTable, instance. This will be used to share information such as element and attribute names between the XML documents and the query. Next we create an XmlReaderSettingsXmlReaderSettingsXmlReaderSettings instance and set its NameTableNameTableNameTable property.
XmlNameTable nameTable = new NameTable(); XmlReaderSettings xmlReaderSettings = new XmlReaderSettings(); xmlReaderSettings.NameTable = nameTable;
In order to query the document we load it into an XdmDocumentXdmDocumentXdmDocument. For more information about document representations, see Document Representations.
XdmDocument document; using (XmlReader reader = XmlReader.Create("bib.xml", xmlReaderSettings)) { document = new XdmDocument(reader); }
Compiling a Query
We will run the following query over our document, which returns the books in ascending order of price.
for $book in /bib/books order by xs:decimal($book/price) return $book
The next stage is to compile the query. In order to describe how the query should be compiled we need set up an XQuerySettingsXQuerySettingsXQuerySettings object. This describes all the settings used for compilation. In particular, we will set the name table used by the query to match the one we used earlier. We can then compile the query using the CompileCompileCompile method. This returns us an XQueryXQueryXQuery object encapsulating the query.
XQuerySettings querySettings = new XQuerySettings(nameTable); string program = "for $book in /bib/books" + "order by xs:decimal($book/price)" + "return $book"; XQuery query = XQuery.Compile(program, querySettings);
An XQueryX query can be compiled by passing an IXPathNavigableIXPathNavigableIXPathNavigable instance over the XQueryX document to the Compile (IXPathNavigable, XQuerySettings)Compile (IXPathNavigable, XQuerySettings)Compile (IXPathNavigable^, XQuerySettings^) method.
Executing the Query
Now we have our query object we now just need to evaluate it. We use the DynamicContextSettingsDynamicContextSettingsDynamicContextSettings which describes the parameters used to evaluate the query. In particular we will set the context item to be the document that we loaded earlier. We then pass these settings to the Evaluate ()Evaluate ()Evaluate () method to evaluate the query.
XPathNavigator contextItem = document.CreateNavigator(); DynamicContextSettings settings = new DynamicContextSettings(); settings.ContextItem = contextItem; XPathItemCollection result = query.Evaluate(settings);
doc
and
collection
functions always raise an error.
This behaviour is changed by setting the
DocumentSetDocumentSetDocumentSet
property on the DynamicContextSettingsDynamicContextSettingsDynamicContextSettings.
See Loading External Resources for more information.
The XPathItemCollectionXPathItemCollectionXPathItemCollection is an enumerable collection of the items returned from the query. Each item returned from the query is either an instance of XPathNavigatorXPathNavigatorXPathNavigator (if the item is a node), or XPathAtomicValueXPathAtomicValueXPathAtomicValue (if the item is an atomic value).
To save the output to a file we should use the
XdmSerializerXdmSerializerXdmSerializer
class with the
XdmSerializationSettingsXdmSerializationSettingsXdmSerializationSettings
as specified by the
SerializationSettingsSerializationSettingsSerializationSettings
on query
. This ensures that any serialization
options specified by the query are used.
For more details on serialization see Serialization