411 REFERENCE · Wikibooks
XQuery
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
XQuery Examples Collection
Welcome to the XQuery Examples Collection Wikibook!
XQuery is a World Wide Web Consortium recommendation for selecting data from documents and databases.
Current Status
2019 refurbishment
There is a Github project to track issues with this book and collaborate amongst eXist db folk at least to bring this resource up-to-date.
Main tasks in the current refurbishment are to:
get example code executable again
remove complex case studies which use obsolete sources
add XQuery 3.0 examples.
Recent Changes
Search
You may search the book here:
center
New and Revised Articles
About this Project
This is a collaborative project and we encourage everyone who is using XQuery to contribute their XQuery examples. All example programs must conform to the creative-commons-2.5 share-alike with attribution license agreement .
Execution of examples use an /eXist demo server/.
Instructors: please sign our /Guest Registry/ if you are using this book for learning or teaching XQuery
Contributors: please see our /Naming Conventions/ to ensure your examples are consistent with the textbook
Learners: If you are looking for an example of a specific XQuery language construct, technique or problem but can't find an example, please add a suggestion to the /Examples Wanted/ section.
Introduction
/Background/ - A brief history and motivation for the XQuery standard. 00% Sept 27th, 2006
/Benefits/ - Why use XQuery? 00% Sept 27th, 2006
/Installing and Testing/ - How to install an XQuery server on your system. 00% Sept 27th, 2006
/Naming Conventions/ - Naming standards used throughout this book. 00% March 17th, 2006
Using XQuery
Common tasks in using XQuery with XML
/Executing a script/ - Executing an XQuery script
/Loading data/ - A tour of several different ways to load data into a native XML database 25% Oct 3rd, 2012
Example Scripts
Beginning Examples
Examples that do not assume knowledge of functions and modules.
/HelloWorld/ - A simple test to see if XQuery is installed correctly. 75% Jan 21st, 2008
/FLWOR Expression/ - A basic example of how XQuery FLWOR statements work. 75% Jan 21st, 2008
/Sequences/ - Working with sequences is central to XQuery. 25% Sept. 26st, 2009
/XPath examples/ - Sample XPath samples for people new to XML and XPath 50% Jan. 26st, 2011
/Regular Expressions/ - Regular expressions make it easy to parse text. 25% Sept. 26st, 2009
/Searching multiple collections/ - How to search multiple collections in a database. 25% Dec 11th, 2008
/Displaying Lists/ - How to take a list of values in an XML structure and return a comma separated list. 50% Nov 6th, 2007
/Extracting data from XHTML files/ - How to use the doc() function to get data from XHTML pages. 25% Aug 6th, 2008
/Displaying data in HTML Tables/ - How to display XML data in an HTML table. 25% Dec 6th, 2007
/Limiting Result Sets/ - How to limit the number of records returned in an XQuery. 25% Sept 6th, 2006
/Filtering Words/ - How to test to see if a word is on a list. 50% Aug 14th, 2007
/Saving and Updating Data/ - How to have a single XQuery that saves new records or updates existing records. 50% Oct 24th, 2007
/Quantified Expressions/ - Testing all the items in a sequence. 25% Oct 29th, 2008
/Dates and Time/ - Sample expressions that work with date and time values 25% May 28th, 2010
Intermediate Examples
Assumes knowledge of functions and modules.
/Using XQuery Functions/ - How to read XQuery function documents and user XQuery functions
/Creating XQuery Functions/ - How to create your own local XQuery functions
/Returning the Longest String/ - A function to find the longest string from a list of strings
/Net Working Days/ - How to calculate the number of working days between two dates 25% Aug 14th, 2007
/Tag Cloud/ - Counting and viewing the number of keywords 75% Aug 17th, 2007
/String Analysis// - Regular expression string analysis
/Manipulating URIs/ - How to get and manage URIs
/Parsing Query Strings/ - Parsing query strings using alternate delimiters. 25% Aug 24th, 2007
/Splitting Files/ - Splitting a large XML files into many smaller files. 25% Aug 24th, 2007
/Filling Portlets/ - How to fill regions of a web page with XQuery
/Filtering Nodes/ - How to use the identity transform to filter out nodes and add nodes 25% Feb 13th, 2008
/Limiting Child Trees/ - You have a tree of information and you want to "prune" only at a specific level 25% Dec 7th, 2010
/Higher Order Functions/ - Passing functions as arguments to functions 25% Nov 10th, 2008
/Timing Fibonacci algorithms/ - A couple of Fibonacci algorithms and timing display
/Using Intermediate Documents/ - Analysis of a MusicXML file
/Formatting Numbers/ - using picture formats to format numbers 100% Nov 18th, 2008
/Uploading Files/ - how to upload files using HTML forms 25% Nov 18th, 2010
/TEI Concordance/ - How to build a TEI-based concordance 25% Nov 18th, 2010
/Queries on Tables/ - How to extract data from tabular data 25% June 28th, 2013
/Namespace Constructors/ - how to dynamically add namespaces when constructing documents 25% December 4th, 2013
Search
/Introduction to XML Search/ - An overview of XML search terminology 25% July 30th, 2009
/Basic Search/ - A simple search page 25% Oct 1st, 2009
/Searching, Paging and Sorting/ - Searching and Viewing search results 25% July 30th, 2009
/Keyword Search/ - full text search with Google-style results 75% July 30th, 2009
/Employee Search/ - an Ajax example 25% July 30th, 2009
/Lucene Search/ - using eXist's Lucene-based fulltext search 25% July 30th, 2009
/Advanced Search/ - creating complex searches using multiple search fields 25% Aug 11th, 2010
/Open Search/ - creating an OpenSearch file to describe your search page 25% Dec 4th, 2010
/Auto-generation of Index Config Files/ - scripts to automatically generate the index configuration file 25% Apr 18th 2011
Transformations
Using XQuery to transform between XML and other representations
/Parsing CSV/ - Using FLWOR on sequences to parse CSV text. 25% Apr 6th, 2012
Interaction
/Getting URL Parameters/ - How to get parameters from the URL. 50% Nov 12th, 2007
/Getting POST Data/ - How to get XML data posted to an XQuery. 50% Nov 12th, 2007
/Checking for Required Parameters/ - How to check for a required parameter using if/then/else. 75% Jan 21st, 2008
/Adder/ - Retaining sate in interactions :Creating a web service that adds two numbers. 25% Sept 6th, 2007
/Chaining Web Forms/ - Passing data from one web page to another using URL parameters, sessions or cookies 25% Dec 17th, 2010
/Simple XForms Examples/
/Incremental Search of the Chemical Elements/ - AJAX 25% Oct 30th, 2010
/Sending E-mail/ - How to send an e-mail message from within an XQuery 25% November 19th, 2009
Visualization
/Graph Visualization with Graphviz/
/Google Chart Sparkline/ - Creating a Tufte Sparkline using the Google Charts API
/Google Chart Bullet Bar/ - Creating a bullet bar using the Google Charts API
Creating Custom Views
These examples use reflection on the structure of an XML document using name() to implement generic functions for XML transformations.
/HTML Table View/ - A generic HTML table representation
/Tree View/ - A generic HTML tree representation
/Grouping Items/ - how to group items in a report
Transforming Complex XML Documents
XQuery has many features that allow you to transform XML and create full document-style transformation libraries. Unlike traditional "database" documents, complex XML documents have "complex content" that includes in-line elements in unpredictable order. This section provides a foundation for these transformations based on using the XQuery typeswitch functions. Typeswitch function transformations replace XSLT transforms but can also access indexes for very fast transforms of large collections. 50% Nov 29th, 2010
/Transformation Styles/ overview of the three styles of transformation 50% July 29th, 2012
/Typeswitch Transformations/ Using the typeswitch function for document-style transforms. 50% December 10th, 2010
/Transformation idioms/ Handling transformation tasks 50% December 18th, 2010
/Generating Skeleton Typeswitch Transformation Modules/ Using XQuery to generate a skeleton module for typeswitch-based document transformation 50% December 10th, 2010
/Web XML Viewer/ Using the typeswitch function to transform an XML document to HTML 25% November 30th, 2010
Paginated Reports
Unlike HTML pages, paginated reports use the concept of text flows between pages. These examples show you how to convert raw XML into high-quality PDF files suitable for printing. The examples use a markup standard called XSL-FO for "Formatted Objects"
/Installing the XSL-FO module/ - update your 1.4 configuration to get the current software from the Apache web site
/Generating PDF from XSL-FO files/ - Converting XML-FO to PDF files 25% September 22nd, 2009
/XSL-FO Tables/ - Generating XSL-FO tables from XML files 25% September 22nd, 2009
/Converting HTML tables to XSL-FO tables/ - use a XQuery typeswitch transform to convert HTML tables to XSL-FO tables 25% February 22nd, 2012
/XSL-FO Images/ - Embedding images in generated (PDF) files
Content Publishing
/Publishing Overview/ - How to transfer a document from an internal intranet server to a public web site 25% February 18th, 2010
/Publishing to Subversion/ - How to transfer a document from an internal intranet to a public SVN server using SSL and digest authentication 25% February 18th, 2010
XML Document Comparison and Merging
/Compare two XML files/ - using the eXist compare() function to test to see if two XML files are exactly the same 75% Oct 17th, 2008
/XML Differences/ - displaying the difference between two XML files 25% Nov 6th, 2008
/Compare with XQuery/ - Using XQuery to Compare Lists 75% May 4th, 2009
/Time Comparison with XQuery/ - Using XQuery to Compare Dated Items 75% May 4th, 2009
/Synchronizing Remote Collections/ - Using lastModified time stamps to see what items have changed 25% May 4th, 2009
/Finding Duplicate Documents/ - Using a hash function to find duplicate documents 75% Dec 17th, 2011
Time Based Queries
/Time Based Queries/ - using dates and times to limit search results 25% Apr 3rd, 2011
/Timing a Query/ - profiling how long a query takes 25% Jun 7th, 2011
XML document kinds
TEI documents
Text Encoding Initiative.
/TEI Concordance/ - How to build a TEI-based concordance 25% Nov 18th, 2010
/TEI Document Timeline/ - Using Simile Timeline to visualize a TEI document
DocBook Documents
/DocBook to HTML/
/DocBook to PDF/
/DocBook to ePub/
/DocBook to Microsoft Word/
OpenOffice
/OpenOffice to HTML/
Office Open XML
/Office Open XML/
XML Schemas
/XML Schema to Instance/
/XML Schema to XForms/
/XML Schema to SVG/
Processing Special Characters
/Special Characters/ - dealing with newlines and other special characters.
XQuery and other languages
MusicXML
/Using Intermediate Documents/ Analysing MusicXML documents
/MusicXML to Arduino/
Language Comparisons
Python
/XQuery and Python/ 25% Jul 23rd, 2008
SQL
/XQuery SQL Module/ - Calling SQL from within your XQuery
/XQuery from SQL/ - Using XQuery to access a classic Relational database - Employee/Department/Salary 25% Jul 23rd, 2008
RDF/OWL
/List OWL Classes/ - A simple XQuery script that will display all the OWL classes in an OWL file 25% May 217th, 2010
Language combination
Excel
/Excel and XML/ 25% Oct 30th, 2010
JavaScript
/Navigating Collections/ - basic AJAX 25% Oct 30th, 2010
/Employee Search/ - basic AJAX 25% Oct 30th, 2010
/DOJO data/ - basic JSON 25% Oct 30th, 2010
SQL
/XML to SQL/ 25% Oct 30th, 2010
Microsoft-Access 25% May 16th, 2014
XHTML + Voice
/Simple RSS reader/ 25% Oct 30th, 2010
/XHTML + Voice/ Twitter Radio for Opera 25% Oct 30th, 2010
XSLT
/XQuery and XSLT/ Executing an XSLT transform from within XQuery 25% Oct 30th, 2010
Data Mashups
Authentication
/Basic Authentication/ - Logging in to a remote web server using HTTP Basic Authentication
/Digest Authentication/ - Logging in to a remote web server using HTTP Digest Authentication
/OAuth/ - A standard for protecting a set of user-owed data within a web service
Wikipedia interaction
/Wikipedia Page scraping/
/Wikipedia Lookup/
/Wikipedia Events RSS/
/Wiki weapons page/
Wikibook applications
/Wikibook index page/
/Wikibook list of code links/
Freebase
/Freebase/ - use XQuery to get data via JSON from Freebase
Google Docs
/Google Docs/ - use XQuery to get data from Google Docs
Visualization
/Graph Visualization/
/Graphing from RDF/
/Dataflow diagrams/
/Sequence Diagrams/
/Example Sequencer/ - Step-by-step tutorial
Google Charts
Although the Google Charts functions only work when you are connected to the Internet, these examples show that
XQuery is an ideal tool for converting XML data into charts.
/Google Charts/ Using XML and XQuery to generate Google Charts using REST 25% Dec. 9th, 2010
/Google Chart Sparkline/ - A demonstration of how to create a chart using the Google Charts API
/Google Chart Bullet Bar/ - A demonstration of how to a dashboard bullet bar using the Google Charts API
/Histogram of File Sizes/ - An XQuery report that generates a histogram of file sizes
There are also sample XForms that can be used to create front-ends in the XForms Tutorial and Cookbook
Digital Dashboards
Digital dashboards are single screens that compress a great deal of information into a single web page. This section will leverage many of the Google Charts examples from the prior section.
Dashboard Architecture - How to design dashboards that have fast response times 25% Dec. 10th, 2010
Page Scraping
Page scraping is the process of extracting well-formed XML data from any HTML web page. When creating mashup applications this is also known as the harvesting process.
/Overview of Page Scraping Techniques/ 25% March 30th, 2009
/Page scraping and Yahoo Weather/
/UK shipping forecast/
/BBC Weather Forecast/
/Page scraping and Mashup/
/Simple RSS reader/
/Multiple page scraping and Voting behaviour/
/Link gathering/
/REST interface definition/
/Caching and indexes/
Mapping
/Google Geocoding/
/String Analysis#Location Mapping/ Mapping Car Registrations
/Flickr GoogleEarth/
/Nationalgrid and Google Maps/
/SMS tracker/
Timelines
/Creating a Timeline/ - Creating a simple timeline view of events 50% March 1st, 2009
/Timelines of Resource/ - Using creation and modification dates to create timelines 25% March 2nd, 2009
/TEI Document Timeline/ - Creating a timeline of all dates within a single TEI document 25% Oct. 2nd, 2010
The Semantic Web
/DBpedia with SPARQL - Football teams/
/DBpedia with SPARQL and Simile Timeline - Album Chronology/ Creating a timeline of album releases using data from Wikipedia 75% Feb 5th, 2009
/DBpedia with SPARQL - Stadium locations/
/The Emp-Dept case study/
/XML to RDF/
/SPARQL Tutorial/
/SPARQL interface/
/Graphing Triples/
/SPARQLing Country Calling Codes/
/Southampton Pubs/
/Alphabet Poster/
/Simile Exhibit/ Browser visualizations using the Simile JavaScript libraries 25% Feb 5th, 2009
/Latent Semantic Indexing/ Finding the semantic distance between documents 25% Feb 5th, 2009
Development Tools
/Sitemap for Content Management System/ XQuery functions can easily perform many common web site content management functions 25% Aug 27st, 2008
/Uptime monitor// use XQuery to monitor a remote web service 25% Nov 18th, 2008
/XQuery IDE/ - XQuery Integrated development environment 25% Nov 18th, 2008
/Image Library/ - using an XQuery to preview your images 25% Nov 18th, 2008
/XML Schema to Instance/ - XQuery function to generate a sample XML instance from an XML Schema file (.xsd) 25% Nov 18th, 2008
/Lorum Ipsum text/ - generating sample text for inserting into test page layouts 25% Nov 18th, 2008
/XQuery and XML Schema/ - Generating an XML instance document 25% Nov 18th, 2008
/Generating XQDocs/ - Automating the generation of XQuery documentation for Modules and Functions 25% Feb 27th, 2010
XqUSEme - Firefox extension to allow XQueries including against the loaded document (even against originally non-XML (poorly formed) HTML).
/Call Graphs/ - Generating an call graph as an SVG file from module introspection 25% July 5th, 2013
/System Properties/ - getting a list of all standard and java system properties and their values 25% March 26th, 2014
/Environment Variables/ - getting a list of all the external environment variables and their values 25% March 26th, 2014
/Generating xqDoc-based XQuery Documentation/ - generating module documentation using the xqDoc commenting standard
Validation
/Validating a document/ - Validate a document with an XML Schema 75% July 29th, 2009
/Validation using a Catalog/ - Using a Catalog file to validate documents 25% July 29th, 2009
/Validating a hierarchy/ - 25% July 29th, 2009
/Validation with Schematron/ - Apply specific rules to check a document
Path Analysis
/All Paths/ - A report of all paths in a document or collection 25% July 22nd, 2010
/All Leaf Paths/ - A report of all leaf paths in a document or collection 25% July 22nd, 2010
Security
/Login and Logout/ - How to log users in and log them out
/URL Driven Authorization/ How to use URL rewriting to check for valid users
/Digital Signatures/ - How to use a custom module to use the XML Digital Signature standards 25% Dec 28nd, 2010
/Changing Permissions on Collections and Resources/ - how to change permissions on collections and resources 25% May 31nd, 2011
Unit Testing Tools
/XUnit Testing/ - what is XUnit testing and how to set them up
/XUnit Annotations/ - how to add annotations to your XQuery functions to drive your XUnit tests 25% May 15t, 2012
Case Studies
/Fizzbuzz/
/Project Euler/
/Topological Sort/
/Slideshow/
/Sudoku/
/Pachube feed/
/World Temperature records/ - conversion of text data formats to XML, indexing and data presentation
/UWE StudentsOnline/
eXist db specific Functions and Configuration
Configuration
Installing modules
/Installing the XSL-FO module/
Setting HTTP Headers
/Setting HTTP Headers/
Modules
dbutil
/Database Utilities/ - a set of database utility functions for database administration
compression
Function Reference
/Get zipped XML file/
/Unzipping an Office Open XML docx file/ - Uncompressing and storing a docx file
eXgit
Module for interacting with any git revision control system
/Installing eXgit/
EXPath File Transfer XQuery Extension Module
This module does both FTP, SFTP (using the SSH protocol) using a standardized and consistent format.
/File Transfer Client/
ftp client
This module allows you to interact with a remote FTP server on a remote system. It includes functions for listing, getting and putting files.
/FTP Client/
httpclient
Function Reference
/Digest Authentication/
/UK shipping forecast/
JSON
/Convert XML to JSON/
Lucene
Function Reference
Help
/Lucene Search/
mail
Function Reference
/Sending E-mail/
/Basic Feedback Form/
math
Function Reference
/Using the Math Module/
memcached
/Using the Memcached Module/
process
/Execute External Process/ - execute a shell command from within XQuery
request
Function Reference
/Getting URL Parameters//
/Getting POST Data//
/Checking for Required Parameters/
/Manipulating URIs/
/Parsing Query Strings/
/Adder/ simple client-server interaction
scheduler
Function Reference
Help
/XQuery Batch Jobs/
sequences
Function Reference
/Sequences Module/ - three additional functions (map, fold and filter)
session
Function Reference
/Basic Session Management/ - the basics of session management including getting a setting session variables 25% Sept 6th, 2007
subversion
Function Reference
/Subversion/ - how to update a subversion repository from within XQuery using the subversion client 25% May 16th, 2011
transform
Function Reference
/String Analysis/
util
Function Reference
/Registered Modules/ : util:registered-modules()
/Registered Functions/ : util:registered-functions()
/Dynamic Module Loading/ : util:import-module(), util:eval()
/Higher Order Functions/ : util:function(), util:call()
/Timing Fibonacci algorithms/ : util:function(), util:call(), util:system-time()
/XMP data/ : util:binary-doc(), util:binary-to-string(), util:parse()
/Basic Authentication/ : util:string-to-binary(), httpclient:get()
validation
Function Reference
Help
/Validating a document/
xmldb
Function Reference
/Saving and Updating Data/
/Splitting Files/
xqdoc
Function Reference
/Generating xqDoc-based XQuery Documentation/
xslfo
XSL-FO (Formatted Objects) is a way of converting XML into PDF.
Function Reference
/Installing the XSL-FO module/ - setting up your XSL-Module within eXist
/Generating PDF from XSL-FO files/ - generating PDF from a FO file
/XSL-FO Tables/ - adding tables to your PDF
/XSL-FO Images/ - adding images to your PDF
/XSL-FO SVG/ - adding SVG images to your PDF
Triggers
/Using Triggers to Log Events/ - how to set up a trigger to log store, update and remove events on a collection 25% July 10th, 2009
/Using Triggers to assign identifiers/ - how to use triggers to assign identifiers to new documents or new nodes 25% September 17th, 2010
/Sending E-mail/ Email is one way to notify when a trigger has fired
XQuery Updates
/Inserting and Updating Attributes/
/Updates and Namespaces/ - How updates can change serialization 25% October 25th, 2009
URL Rewriting
/URL Rewriting Basics/ How to make your URLs look nice 25% November 4th, 2009
Apache Ant Tasks
/Reindex a Collection/
Replication
/eXist Replication /
General guidance
/eXist Crib sheet/
eXist-db maintenance - delete versions of binary files
Appendixes
Systems that Support XQuery
Using native and hybrid XML databases that implement XQuery 00% Feb 15th, 2007
BaseX - Native open source XML Database with visual frontend 00% Feb 1st, 2010
/DataDirect XQuery/ - Java XQuery engine supporting relational, EDI, flat files and XML input/output
eXist - Open source native XML database 00% Feb 15th, 2007
DB2 pureXML - DB2 9.1 includes the pureXML feature 00% Mar 19th, 2007
MarkLogic Server - MarkLogic Server commercial XML Content Server 00% Feb 15th, 2007
/Microsoft SQL Server 2005/ - 00% Mar 19th, 2007
/NetKernel/ - NetKernel 00% Feb 15th, 2007
/Oracle Berkeley DB XML/ - Open source embedded storage management 00% Nov 5th, 2008
Oracle XML DB - Oracle Server 11g includes the XML DB (XDB) feature 00% Nov 5th, 2008
Sedna - Open source native XML Database 00% Jov 1st, 2007
/Stylus Studio/ - XQuery mapping/editing/debugging, ships with Saxon (and SA) and DataDirect XQuery
EMC xDB - EMC Documentum xDB commercial native XML database 00% Dec 17th, 2008
/XQilla/ - Open source XQuery library and command line utility 00% Nov 5th, 2008
Zorba - Open source XQuery engine C++ implementation with C, Java, Php, Python, Ruby library bindings and command line utility
Qizx - Open source and pro XQuery engine Java implementation
Debugging XQuery
/Gotchas/ - some pitfalls
/Ah-has// - some ah-ha moments
Other sources
Function Libraries
FunctX XQuery Function Library by Priscilla Walmsley
Discussion Groups
XQuery General
Indexes
/Index of Application Areas/ - edited
Key to symbols:
15px indicates an XQuery/Best practice
Computer programming languages
X
50%
fr:XQuery