Skip to main content

Books

Querying Your XML Documents

This example will reference the bib.xml document from the Use Case "XMP"2, also referenced in the aforementioned book1.

Books

Query Q1 from the W3C Use Case "XMP" lists all books published by Addison-Wesley after 1991:

 
 <bib> { 
   for $b in doc("http://monetdb.cwi.nl/XQuery/files/bib.xml")/bib/book 
   where $b/publisher = "Addison-Wesley" 
     and $b/@year > 1991 
   return 
     <book year="{ $b/@year }"> 
       { $b/title } 
     </book> 
 } </bib> 

The query will return a bib element that contains two book children.


The following query books2.xq creates a table of contents from the book.xml document.

declare function local:toc($book-or-section as element()) as element()*
{
    for $section in $book-or-section/section
    return
      <section>
         { $section/@* , $section/title , local:toc($section) }                 
      </section>
};
<toc>
   {
     for $s in doc("http://monetdb.cwi.nl/XQuery/files/book.xml")/book return local:toc($s)
   }
</toc> 

1XQuery from the Experts. A Guide to the W3C XML Query Language. Howard Katz et al., Addison Wesley, Boston 2004.

2XML Query Use Cases. Don Chamberlin et al. W3C Working Draft.

File Caching in MonetDB/XQuery

MonetDB/XQuery automatically caches XML documents with file URLs, i.e. those that start with file:// or without naming scheme (because the default interpretation of an URI is a file path).

This means the second time you query the same document, performance will be better, because the file does not need to be read anymore, it will already be stored and indexed by MonetDB/XQuery.

There is a limit to the size of the XML cache, so files may not be cached forever. MonetDB/XQuery always checks the modification time of the file, such if it was modified on disk, it gets removed from the cache.

The documentation provides more information on how the XML cache works and can be configured.

How to add documents persistently to the database

Typing in the full path of a document for each query may be cumbersome, and only temporarily stores the document in the document cache. This means the system may throw it away, and the next query using it will have to retrieve the XML file again.

To learn how to add a document persistently to your XML database under a short alias name, read our quick guide to Document Management.