O'Reilly logo

XQuery, 2nd Edition by Priscilla Walmsley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required


This book provides complete coverage of the W3C XQuery 3.1 standard. In addition, it provides the background knowledge in namespaces, schemas, built-in types, and regular expressions that is relevant to writing XML queries.

This book is designed for query writers who have some knowledge of XML basics but not necessarily advanced knowledge of XML-related technologies. It can be used as a tutorial, by reading it cover to cover, and as a reference, by using the comprehensive index and appendixes.

Contents of This Book

The book is organized into six parts:

  1. Chapters 1 and 2 provide a high-level overview and quick tour of XQuery.

  2. Chapters 3 through 9 provide enough information to write sophisticated queries, without being bogged down by the details of types, namespaces, and schemas.

  3. Chapters 10 through 16 introduce some advanced concepts for users who want to take advantage of modularity, namespaces, typing, and schemas.

  4. Chapters 17 through 24 provide guidelines for working with specific types of data, such as numbers, strings, dates, URIs, processing instructions, and maps.

  5. Chapters 25 through 28 describe XQuery’s implementation-specific features and its relationship to other standards, including SQL and XSLT.

  6. Appendices A, B, and C provide a complete alphabetical reference to the built-in functions, types, and error messages.

Reading the Syntax Diagrams

This book includes syntax diagrams as an option for readers who want a more visual representation of XQuery expression syntax. Each syntax diagram is accompanied by explanatory text and examples. Figure P-1 illustrates the components of a syntax diagram, showing the schema import syntax as an example.

Figure P-1. Example syntax diagram

Rules for interpreting the syntax diagrams are:

  • Parts of the diagram in constant width font are literal values. In Figure P-1, import schema and at should appear literally in your query.

  • Quotes that appear in syntax diagrams also must appear in your query. Figure P-1 shows that the <namespace-name> must be surrounded by quotes, whereas the <prefix> must not. Either single or double quotes can be used in XQuery, but only double quotes are included in the diagrams for simplicity.

  • Where you can specify a value, such as a name, a descriptive name for that value appears in constant width italic and is surrounded by angle brackets. Figure P-1 shows that you fill in the <namespace-name>, <prefix>, and <location> with your own values.

  • Multiple options are indicated by parallel lines in the diagram. Figure P-1 shows that you may choose to specify a namespace prefix or default element namespace.

  • Optional parts of the expression are indicated by a line that bypasses the main arrow. In Figure P-1, it is not necessary to include the namespace <prefix> = or the default element namespace keywords.

  • Repeating parts of an expression are indicated by an arrow that returns to the beginning. Figure P-1 shows that you can specify multiple <location>s (separated by commas) as part of the at clause.

Conventions Used in This Book

Constant width is used for:

  • Code examples and fragments

  • Anything that might appear in an XML document, including element and attribute names, element contents, attribute values, and processing instructions

  • Anything that might appear in a query, including keywords, operators, and literals

Constant width bold is used for:

  • Emphasis in code examples and fragments

Italic is used for:

  • New terms where they are defined

  • Emphasis in body text

  • Pathnames, filenames, and program names

  • Host and domain names


This icon indicates a warning or caution.

The numbered examples in this book are self-sufficient and contain all the necessary declarations to run them independently. When they use functionality that is only available in XQuery 3.0 or 3.1, they contain a version declaration to indicate this. Other (non-numbered) examples appear as code fragments that may not be able to run independently because, for example, they are dependent on a variable that is defined elsewhere. These code fragments are placed into a separate paragraph like this:

for $prod as element(*, ProductType) in doc("catalog.xml")/catalog/*
return $prod/name

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at http://www.datypic.com/books/xquery/examples.html.

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a query that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "XQuery by Priscilla Walmsley. Copyright 2016 Priscilla Walmsley, 978-1-491-91510-3.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Useful Functions

This book contains a series of illustrative examples that are labeled “Useful Function.” What sets them apart from regular examples is that they are likely to be directly useful in your own queries. They range from string functions like substring-after-last and replace-first to functions that modify elements and attributes, such as add-attributes.

The useful functions included in this book are part of a large library of XQuery functions called the FunctX XQuery Library, which is available at http://www.xqueryfunctions.com. This library contains a wide variety of reusable XQuery functions that can be searched or browsed by category. It also includes detailed descriptions and example function calls.

O’Reilly Safari


Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.

For more information, please visit http://oreilly.com/safari.

We’d Like to Hear from You

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

O’Reilly has a web page for this book at http://oreil.ly/1lgtvMF. The author also maintains a companion web site at http://www.datypic.com/books/xquery/ that lists errata, examples, and additional information.

To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our web site at:

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia


I am deeply indebted to Michael Kay, not only for his detailed review of this book, but also for his excellent Saxon XQuery implementation, without which I would not have been able to reliably test the examples.

Ron Bourret, Bob DuCharme, Tim Finney, Ashok Malhotra, Darin McBeath, Peter Meggitt, Shannon Shiflett, and Bruno J. Walmsley (my father) provided extremely helpful comments on, and assistance with, the first edition of this book. Michael Kay, Debbie Lockett, Dave Pawson, and Adam Retter provided especially helpful comments on the second edition of this book. I would also like to thank Steve Carton, Patrick Durusau, Betty Harvey, Eliot Kimber, Greg Murray, Liam Quin, Hans-Jürgen Rennau, Ken Sall, Christine Schwartz, Will Thompson, and Joe Wicentowski for their feedback and guidance on the second edition.

This project would not have been possible without Simon St.Laurent, who provided editorial guidance and championed the book within O’Reilly. Meg Foley was my excellent (and very patient!) editor for the second edition.

Finally, I would like to thank Doug, my partner, my love, for his constant support and encouragement.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required