0%

Book Description

The W3C XQuery 3.1 standard provides a tool to search, extract, and manipulate content, whether it's in XML, JSON or plain text. With this fully updated, in-depth tutorial, you’ll learn to program with this highly practical query language. Designed for query writers who have some knowledge of XML basics, but not necessarily advanced knowledge of XML-related technologies, this book is ideal as both a tutorial and a reference. You’ll find background information for namespaces, schemas, built-in types, and regular expressions that are relevant to writing XML queries.

Table of Contents

  1. Preface
    1. Contents of This Book
    2. Reading the Syntax Diagrams
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Useful Functions
    6. O’Reilly Safari
    7. We’d Like to Hear from You
    8. Acknowledgments
  2. 1. Introduction to XQuery
    1. What Is XQuery?
      1. Capabilities of XQuery
      2. Uses for XQuery
      3. Processing Scenarios
    2. Easing into XQuery
    3. Path Expressions
    4. FLWORs
    5. Adding XML Elements and Attributes
      1. Adding Elements
      2. Adding Attributes
    6. Functions
    7. Joins
    8. Aggregating and Grouping Values
  3. 2. XQuery Foundations
    1. The Design and History of the XQuery Language
    2. XQuery in Context
      1. XQuery and XPath
      2. XQuery Versus XSLT
      3. XQuery Versus SQL
      4. XQuery and XML Schema
    3. Processing Queries
      1. Input Documents
      2. The Query
      3. The Context
      4. The Query Processor
      5. The Results of the Query
    4. The XQuery Data Model
      1. Nodes
      2. Atomic Values
      3. Sequences
    5. Types
    6. Namespaces
  4. 3. Expressions: XQuery Building Blocks
    1. Categories of Expressions
    2. Keywords and Names
    3. Whitespace in Queries
    4. Literals
    5. Variables
    6. Function Calls
    7. Comments
    8. Precedence and Parentheses
    9. Comparison Expressions
      1. General Comparisons
      2. Value Comparisons
      3. Node Comparisons
    10. Conditional (if-then-else) Expressions
      1. Conditional Expressions and Effective Boolean Value
      2. Nesting Conditional Expressions
    11. Switch Expressions
    12. Logical (and/or) Expressions
      1. Precedence of Logical Expressions
      2. Negating a Boolean Value
  5. 4. Navigating XML by Using Paths
    1. Path Expressions
      1. Path Expressions and Context
      2. Steps
      3. Axes
      4. Node Tests
      5. Abbreviated Syntax
      6. Other Expressions as Steps
    2. Predicates
      1. Comparisons in Predicates
      2. Using Positions in Predicates
      3. Using Multiple Predicates
      4. More Complex Predicates
    3. A Closer Look at Context
      1. Working with the Context Node
      2. Accessing the Root
    4. Dynamic Paths
    5. The Simple Map Operator
  6. 5. Adding Elements and Attributes to Results
    1. Including Elements and Attributes from the Input Document
    2. Direct Element Constructors
      1. Containing Literal Characters
      2. Containing Other Element Constructors
      3. Containing Enclosed Expressions
      4. Specifying Attributes Directly
      5. Declaring Namespaces in Direct Constructors
      6. Use Case: Modifying an Element from the Input Document
      7. Direct Element Constructors and Whitespace
    3. Computed Constructors
      1. Computed Element Constructors
      2. Computed Attribute Constructors
      3. Use Case: Turning Content to Markup
  7. 6. Selecting and Joining Using FLWORs
    1. Selecting with Path Expressions
    2. FLWOR Expressions
      1. The for Clause
      2. The let Clause
      3. The where Clause
      4. The return Clause
      5. The Scope of Variables
    3. Quantified Expressions
      1. Binding Multiple Variables
    4. Selecting Distinct Values
    5. Joins
      1. Three-Way Joins
      2. Outer Joins
      3. Joins and Types
  8. 7. Sorting and Grouping
    1. Sorting in XQuery
      1. The order by Clause
      2. The sort Function
      3. Document Order
      4. Document Order Comparisons
      5. Reversing the Order
      6. Indicating That Order Is Not Significant
    2. Grouping
      1. Grouping Using the group by Clause
    3. Aggregating Values
      1. Ignoring “Missing” Values
      2. Counting “Missing” Values
      3. Aggregating on Multiple Values
      4. Constraining and Sorting on Aggregated Values
  9. 8. Functions
    1. Built-in Versus User-Defined Functions
    2. Calling Functions
      1. Function Names
      2. Function Signatures
      3. Argument Lists
      4. Sequence Types
      5. Calling Functions with the Arrow Operator
    3. User-Defined Functions
      1. Why Define Your Own Functions?
      2. Function Declarations
      3. The Function Body
      4. The Function Name
      5. The Parameter List
      6. Functions and Context
      7. Recursive Functions
  10. 9. Advanced Queries
    1. Working with Positions and Sequence Numbers
      1. Adding Sequence Numbers to Results
      2. Using the count Clause
      3. Testing for the Last Item
    2. Windowing
      1. Using start and end Conditions
      2. Windows Based on Position
      3. Windows Based on Previous or Next Items
      4. Sliding Windows
    3. Copying Input Elements with Modifications
      1. Adding Attributes to an Element
      2. Removing Attributes from an Element
      3. Removing Attributes from All Descendants
      4. Removing Child Elements
      5. Changing Names
    4. Combining Results
      1. Sequence Constructors
      2. The union Expression
      3. The intersect Expression
      4. The except Expression
    5. Using Intermediate XML Documents
      1. Creating Lookup Tables
      2. Reducing Complexity
  11. 10. Namespaces and XQuery
    1. XML Namespaces
      1. Namespace URIs
      2. Declaring Namespaces
      3. Default Namespace Declarations
      4. Namespaces and Attributes
      5. Namespace Declarations and Scope
    2. Namespaces and XQuery
    3. Namespace Declarations in Queries
      1. Predeclared Namespaces
      2. Prolog Namespace Declarations
      3. Namespace Declarations in Direct Element Constructors
      4. Namespace Declarations in Computed Constructors
      5. The Impact and Scope of Namespace Declarations
    4. Controlling Namespace Declarations in Your Results
      1. In-Scope Versus Statically Known Namespaces
      2. Controlling the Copying of Namespace Declarations
    5. URI-Qualified Names
  12. 11. A Closer Look at Types
    1. The XQuery Type System
      1. Advantages of a Strong Type System
      2. Do You Need to Care About Types?
    2. The Built-in Types
      1. Atomic Types
      2. List Types
      3. Union Types
    3. Types, Nodes, and Atomic Values
      1. Nodes and Types
      2. Atomic Values and Types
    4. Type Checking in XQuery
      1. The Static Analysis Phase
      2. The Dynamic Evaluation Phase
    5. Automatic Type Conversions
      1. Subtype Substitution
      2. Type Promotion
      3. Casting of Untyped Values
      4. Atomization
      5. Effective Boolean Value
      6. Function Conversion Rules
    6. Sequence Types
      1. Occurrence Indicators
      2. Generic Sequence Types
      3. Simple Type Names as Sequence Types
      4. Element and Attribute Tests
      5. Sequence Type Matching
      6. The instance of Expression
    7. Constructors and Casting
      1. Constructors
      2. The Cast Expression
      3. The Castable Expression
      4. Casting Rules
  13. 12. Prologs, Modules, and Variables
    1. Structure of a Query: Prolog and Body
      1. Prolog Declarations
      2. The Version Declaration
    2. Assembling Queries from Multiple Modules
      1. Library Modules
      2. Importing a Library Module
      3. Loading a Library Module Dynamically
    3. Variable Declarations
      1. Variable Declaration Syntax
      2. The Scope of Variables
      3. Variable Names
      4. Initializing Expressions
      5. External Variables
    4. Private Functions and Variables
    5. Declaring External Functions
  14. 13. Inputs and Outputs
    1. Types of Input and Output Documents
    2. Accessing Input Documents
      1. Accessing a Single Document with a Function
      2. Accessing a Collection
      3. Setting the Context Outside the Query
      4. Using Variables
      5. Setting the Context in the Prolog
    3. Serializing Output
      1. Serialization Methods
      2. Serialization Parameters
      3. Specifying Serialization Parameters by Using Option Declarations
      4. Specifying Serialization Parameters by Using a Separate XML Document
      5. Specifying Serialization Parameters by Using a Map
      6. Serialization Errors
      7. Serializing to a String
  15. 14. Using Schemas with XQuery
    1. What Is a Schema?
    2. Why Use Schemas with Queries?
    3. W3C XML Schema: A Brief Overview
      1. Element and Attribute Declarations
      2. Types
      3. Namespaces and XML Schema
    4. In-Scope Schema Definitions
      1. Where Do In-Scope Schema Definitions Come From?
      2. Schema Imports
    5. Schema Validation and Type Assignment
      1. The Validate Expression
      2. Validation Mode
      3. Assigning Type Annotations to Nodes
      4. Nodes and Typed Values
      5. Types and Newly Constructed Elements and Attributes
    6. Sequence Types and Schemas
  16. 15. Static Typing
    1. What Is Static Typing?
      1. Obvious Static Type Errors
      2. Static Typing and Schemas
      3. Raising “False” Errors
      4. Static Typing Expressions and Constructs
    2. The Typeswitch Expression
    3. The Treat Expression
    4. Type Declarations
      1. Type Declarations in FLWORs
      2. Type Declarations in Quantified Expressions
      3. Type Declarations in Global Variable Declarations
    5. The zero-or-one, one-or-more, and exactly-one Functions
  17. 16. Writing Better Queries
    1. Query Design Goals
    2. Clarity
      1. Improving the Layout
      2. Choosing Names
      3. Using Comments for Documentation
    3. Modularity
    4. Robustness
      1. Handling Data Variations
      2. Handling Missing Values
    5. Error Handling
      1. Avoiding Dynamic Errors
      2. The error and trace Functions
      3. Try/Catch Expressions
    6. Performance
      1. Avoid Reevaluating the Same or Similar Expressions
      2. Avoid Unnecessary Sorting
      3. Avoid Expensive Path Expressions
      4. Use Predicates Instead of where Clauses
  18. 17. Working with Numbers
    1. The Numeric Types
      1. The xs:decimal Type
      2. The xs:integer Type
      3. The xs:float and xs:double Types
      4. The xs:numeric Type
    2. Constructing Numeric Values
      1. The number Function
      2. Numeric Type Promotion
    3. Comparing Numeric Values
    4. Arithmetic Operations
      1. Arithmetic Operations on Multiple Values
      2. Arithmetic Operations and Types
      3. Precedence of Arithmetic Operators
      4. Addition, Subtraction, and Multiplication
      5. Division
      6. Modulus (Remainder)
    5. Functions on Numbers
    6. Formatting Numbers
      1. Formatting Integers
      2. Formatting Decimal Numbers
      3. The Decimal Format Declaration
  19. 18. Working with Strings
    1. The xs:string Type
    2. Constructing Strings
      1. String Literals
      2. The xs:string Constructor and the string Function
      3. String Constructors
    3. Comparing Strings
      1. Comparing Entire Strings
      2. Determining Whether a String Contains Another String
      3. Matching a String to a Pattern
    4. Substrings
    5. Finding the Length of a String
    6. Concatenating and Splitting Strings
      1. Concatenating Strings
      2. Splitting Strings Apart
      3. Converting Between Codepoints and Strings
    7. Manipulating Strings
      1. Converting Between Uppercase and Lowercase
      2. Replacing Individual Characters in Strings
      3. Replacing Substrings That Match a Pattern
    8. Whitespace and Strings
      1. Normalizing Whitespace
    9. Internationalization Considerations
      1. Collations
      2. Unicode Normalization
      3. Determining the Language of an Element
  20. 19. Regular Expressions
    1. The Structure of a Regular Expression
      1. Atoms
      2. Quantifiers
      3. Parenthesized Sub-Expressions and Branches
    2. Representing Individual Characters
    3. Representing Any Character
    4. Representing Groups of Characters
      1. Multi-Character Escapes
      2. Category Escapes
      3. Block Escapes
    5. Character Class Expressions
      1. Single Characters and Ranges
      2. Subtraction from a Range
      3. Negative Character Class Expressions
      4. Escaping Rules for Character Class Expressions
    6. Reluctant Quantifiers
    7. Anchors
    8. Back-References
    9. Using Flags
    10. Using Sub-Expressions with Replacement Variables
  21. 20. Working with Dates, Times, and Durations
    1. The Date and Time Types
      1. Constructing and Casting Dates and Times
      2. Time Zones
      3. Comparing Dates and Times
    2. The Duration Types
      1. The xs:yearMonthDuration and xs:dayTimeDuration Types
      2. Comparing Durations
    3. Extracting Components of Dates, Times, and Durations
    4. Formatting Dates and Times
    5. Using Arithmetic Operators on Dates, Times, and Durations
      1. Subtracting Dates and Times
      2. Adding and Subtracting Durations from Dates and Times
      3. Adding and Subtracting Two Durations
      4. Multiplying and Dividing Durations by Numbers
      5. Dividing Durations by Durations
    6. The Date Component Types
  22. 21. Working with Qualified Names, URIs, and IDs
    1. Working with Qualified Names
      1. Retrieving Node Names
      2. Constructing Qualified Names
      3. Other Name-Related Functions
    2. Working with URIs
      1. Base and Relative URIs
      2. Documents and URIs
      3. Escaping URIs
    3. Working with IDs
      1. Joining IDs and IDREFs
      2. Constructing ID Attributes
      3. Generating Unique ID Values
  23. 22. Working with Other XML Constructs
    1. XML Comments
      1. XML Comments and the Data Model
      2. Querying Comments
      3. Comments and Sequence Types
      4. Constructing Comments
    2. Processing Instructions
      1. Processing Instructions and the Data Model
      2. Querying Processing Instructions
      3. Processing Instructions and Sequence Types
      4. Constructing Processing Instructions
    3. Documents
      1. Document Nodes and the Data Model
      2. Document Nodes and Sequence Types
      3. Constructing Document Nodes
    4. Text Nodes
      1. Text Nodes and the Data Model
      2. Querying Text Nodes
      3. Text Nodes and Sequence Types
      4. Why Work with Text Nodes?
      5. Constructing Text Nodes
    5. XML Entity and Character References
    6. CDATA Sections
  24. 23. Function Items and Higher-Order Functions
    1. Why Higher-Order Functions?
    2. Constructing Functions and Calling Them Dynamically
      1. Named Function References
      2. Using function-lookup to Obtain a Function
      3. Inline Function Expressions
      4. Partial Function Application
      5. The Arrow Operator and Dynamic Function Calls
      6. Syntax Recap
    3. Functions and Sequence Types
    4. Higher-Order Functions
      1. Built-In Higher-Order Functions
      2. Writing Your Own Higher-Order Functions
  25. 24. Maps, Arrays, and JSON
    1. Maps
      1. Constructing Maps
      2. Looking Up Map Values
      3. Querying Maps
      4. Changing Maps
      5. Iterating over Entries in a Map
      6. Maps and Sequence Types
    2. Arrays
      1. Constructing Arrays
      2. Arrays Versus Sequences
      3. Arrays and Atomization
      4. Looking Up Array Values
      5. Querying Arrays
      6. Changing Arrays
      7. Arrays and Sequence Types
    3. JSON
      1. Parsing JSON
      2. Serializing JSON
      3. Converting Between JSON and XML
  26. 25. Implementation-Specific Features
    1. Conformance
    2. Version Support
      1. New Features in XQuery 3.0
      2. New Features in XQuery 3.1
    3. Setting the Query Context
    4. The Option Declaration
    5. Extension Expressions
    6. Annotations
  27. 26. XQuery for SQL Users
    1. Relational Versus XML Data Models
    2. Comparing SQL Syntax with XQuery Syntax
      1. A Simple Query
      2. Conditions and Operators
      3. Functions
      4. Selecting Distinct Values
      5. Working with Multiple Tables and Subqueries
      6. Grouping
    3. Combining SQL and XQuery
      1. Combining Structured and Semi-Structured Data
      2. Flexible Data Structures
    4. SQL/XML
  28. 27. XQuery for XSLT Users
    1. XQuery and XPath
    2. XQuery Versus XSLT
      1. Shared Components
      2. Equivalent Components
      3. Differences
    3. Using XQuery and XSLT Together
    4. XQuery Backward Compatibility with XPath 1.0
      1. Data Model
      2. New Expressions
      3. Path Expressions
      4. Function Conversion Rules
      5. Arithmetic and Comparison Expressions
      6. Built-in Functions
  29. 28. Additional XQuery-Related Standards
    1. XQuery Update Facility
    2. Full-Text Search
    3. XQueryX
    4. RESTXQ
    5. XQuery API for Java (XQJ)
  30. A. Built-in Function Reference
  31. B. Built-in Types
    1. xs:anyAtomicType
    2. xs:anySimpleType
    3. xs:anyType
    4. xs:anyURI
      1. Casting and Comparing xs:anyURI Values
    5. xs:base64Binary
    6. xs:boolean
      1. Constructing xs:boolean Values
      2. Casting xs:boolean Values
    7. xs:byte
    8. xs:date
    9. xs:dateTime
    10. xs:dateTimeStamp
    11. xs:dayTimeDuration
    12. xs:decimal
      1. Casting xs:decimal Values
    13. xs:double
      1. Casting xs:double Values
    14. xs:duration
    15. xs:ENTITIES
    16. xs:ENTITY
    17. xs:error
    18. xs:float
      1. Casting xs:float Values
    19. xs:gDay
    20. xs:gMonth
    21. xs:gMonthDay
    22. xs:gYear
    23. xs:gYearMonth
    24. xs:hexBinary
    25. xs:ID
    26. xs:IDREF
    27. xs:IDREFS
    28. xs:int
    29. xs:integer
      1. Casting xs:integer Values
    30. xs:language
    31. xs:long
    32. xs:Name
    33. xs:NCName
    34. xs:negativeInteger
    35. xs:NMTOKEN
    36. xs:NMTOKENS
    37. xs:nonNegativeInteger
    38. xs:nonPositiveInteger
    39. xs:normalizedString
    40. xs:NOTATION
    41. xs:numeric
    42. xs:positiveInteger
    43. xs:QName
    44. xs:short
    45. xs:string
    46. xs:time
    47. xs:token
    48. xs:unsignedByte
    49. xs:unsignedInt
    50. xs:unsignedLong
    51. xs:unsignedShort
    52. xs:untyped
    53. xs:untypedAtomic
    54. xs:yearMonthDuration
  32. C. Error Summary
    1. FOAP0001
    2. FOAR0001
    3. FOAR0002
    4. FOAY0001
    5. FOAY0002
    6. FOCA0001
    7. FOCA0002
    8. FOCA0003
    9. FOCA0005
    10. FOCA0006
    11. FOCH0001
    12. FOCH0002
    13. FOCH0003
    14. FOCH0004
    15. FODC0001
    16. FODC0002
    17. FODC0003
    18. FODC0004
    19. FODC0005
    20. FODC0006
    21. FODC0010
    22. FODF1280
    23. FODF1310
    24. FODT0001
    25. FODT0002
    26. FODT0003
    27. FOER0000
    28. FOFD1340
    29. FOFD1350
    30. FOJS0001
    31. FOJS0003
    32. FOJS0004
    33. FOJS0005
    34. FOJS0006
    35. FOJS0007
    36. FONS0004
    37. FONS0005
    38. FOQM0001
    39. FOQM0002
    40. FOQM0003
    41. FOQM0005
    42. FOQM0006
    43. FORG0001
    44. FORG0002
    45. FORG0003
    46. FORG0004
    47. FORG0005
    48. FORG0006
    49. FORG0008
    50. FORG0009
    51. FORG0010
    52. FORX0001
    53. FORX0002
    54. FORX0003
    55. FORX0004
    56. FOTY0012
    57. FOTY0013
    58. FOTY0014
    59. FOTY0015
    60. FOUT1170
    61. FOUT1190
    62. FOUT1200
    63. FOXT0001
    64. FOXT0002
    65. FOXT0003
    66. FOXT0004
    67. FOXT0006
    68. SENR0001
    69. SEPM0004
    70. SEPM0009
    71. SEPM0010
    72. SEPM0016
    73. SEPM0017
    74. SEPM0018
    75. SEPM0019
    76. SERE0003
    77. SERE0005
    78. SERE0006
    79. SERE0008
    80. SERE0012
    81. SERE0014
    82. SERE0015
    83. SERE0020
    84. SERE0021
    85. SERE0022
    86. SERE0023
    87. SESU0007
    88. SESU0011
    89. SESU0013
    90. XPDY0002
    91. XPDY0050
    92. XPDY0130
    93. XPST0001
    94. XPST0003
    95. XPST0005
    96. XPST0008
    97. XPST0017
    98. XPST0051
    99. XPST0080
    100. XPST0081
    101. XPTY0004
    102. XPTY0018
    103. XPTY0019
    104. XPTY0020
    105. XPTY0117
    106. XQDY0025
    107. XQDY0026
    108. XQDY0027
    109. XQDY0041
    110. XQDY0044
    111. XQDY0054
    112. XQDY0061
    113. XQDY0064
    114. XQDY0072
    115. XQDY0074
    116. XQDY0084
    117. XQDY0091
    118. XQDY0092
    119. XQDY0096
    120. XQDY0101
    121. XQDY0102
    122. XQDY0137
    123. XQST0009
    124. XQST0012
    125. XQST0013
    126. XQST0016
    127. XQST0022
    128. XQST0031
    129. XQST0032
    130. XQST0033
    131. XQST0034
    132. XQST0035
    133. XQST0038
    134. XQST0039
    135. XQST0040
    136. XQST0045
    137. XQST0046
    138. XQST0047
    139. XQST0048
    140. XQST0049
    141. XQST0052
    142. XQST0055
    143. XQST0057
    144. XQST0058
    145. XQST0059
    146. XQST0060
    147. XQST0065
    148. XQST0066
    149. XQST0067
    150. XQST0068
    151. XQST0069
    152. XQST0070
    153. XQST0071
    154. XQST0075
    155. XQST0076
    156. XQST0079
    157. XQST0085
    158. XQST0087
    159. XQST0088
    160. XQST0089
    161. XQST0090
    162. XQST0094
    163. XQST0097
    164. XQST0098
    165. XQST0099
    166. XQST0103
    167. XQST0104
    168. XQST0106
    169. XQST0108
    170. XQST0109
    171. XQST0110
    172. XQST0111
    173. XQST0113
    174. XQST0114
    175. XQST0115
    176. XQST0116
    177. XQST0118
    178. XQST0119
    179. XQST0125
    180. XQST0129
    181. XQST0134
    182. XQTY0024
    183. XQTY0030
    184. XQTY0086
    185. XQTY0105
  33. Index
18.209.209.246