XQuery: Search Across a Variety of XML Data, 2nd Edition

XQuery: Search Across a Variety of XML Data, 2nd Edition

English | 2015 | ISBN: 978-1491915103 | 736 Pages | PDF, EPUB | 10 MB


With XQuery, you finally have a tool that will make it much easier to search, extract and manipulate information from XML content stored in databases. Updated for XQuery 3.1, this in-depth tutorial not only walks you through the XQuery specification, but also teaches you how to program with this highly practical query language.
XQuery is for query writers who have some knowledge of XML basics, but not necessarily advanced knowledge of XML-related technologies. It can be used both as a tutorial, by reading cover to cover, and as a reference, by using the comprehensive index and appendixes. Either way, you will find the background knowledge in namespaces, schemas, built-in types and regular expressions that is relevant to writing XML queries.
This second edition provides:

  • A high-level overview and quick tour of XQuery
  • New chapters on higher-order functions, maps, arrays, and JSON
  • Information to write sophisticated queries, without being bogged down by the details of types, namespaces, and schemas
  • Advanced concepts for users who want to take advantage of modularity, namespaces, typing and schemas
  • Guidelines for working with specific types of data, such as numbers, strings, dates, URIs and processing instructions
  • A complete alphabetical reference to the built-in functions and types

You’ll also learn about XQuery’s support for filtering, sorting, and grouping data, as well as how to use FLWOR expressions, XPath, and XQuery tools for extracting and combining information. Discover how to apply all of these tools to a wide variety of data sources, and how to recombine information from multiple sources into a single final output result.

+

Maps, Arrays and JSON

Sequence types, which are discussed in detail in “Sequence Types” on page 210, are used in a variety of XQuery expressions to indicate allowed values. The most common use is in the signatures of user-defined functions. For example, to write a function that takes an array and returns those members that are greater than 50, you could use the function shown in Example 24-11. The use of the array(xs:integer) sequence type in the function signature ensures that only a single array whose members are all integers are passed to this function as the $arrayarg arg

JSON

Although maps and arrays are useful on their own as programming constructs, one of the important reasons for adding them to the XQuery specification was to support the querying of JSON documents, and the ability to return results as JSON objects. XQuery map entries correspond nicely to JSON properties in that they have to have unique keys/names, their order doesn’t matter, and they can have complex structures. Table 24-1 provides a general mapping between JSON and the XQuery data model.

XQuery for SQL Users

To constrain the query based on the contents of the desc column, you can use an XQuery expression embedded in your SQL statement. In the case of SQL Server, one way to do this is through the use of an exist function. For example:

The SQL Server exist function returns true (which equals 1) if there are any nodes that match the criteria. The expression in parentheses passed to the exist function is in XQuery syntax, and it is evaluated relative to the root of the document in the prop erties column, for each row. The results are shown in Table 27-9.