Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Preface
Contents of This Book
Reading the Syntax Diagrams
Conventions Used in This Book
Using Code Examples
Useful Functions
We’d Like to Hear from You
Acknowledgments
1. Introduction to XQuery
What Is XQuery?
Capabilities of XQuery
Uses for XQuery
Processing Scenarios
Easing into XQuery
Path Expressions
FLWORs
Adding XML Elements and Attributes
Adding Elements
Adding Attributes
Functions
Joins
Aggregating and Grouping Values
2. XQuery Foundations
The Design of the XQuery Language
XQuery in Context
XQuery and XPath
XQuery Versus XSLT
XQuery Versus SQL
XQuery and XML Schema
Processing Queries
Input Documents
The Query
The Context
The Query Processor
The Results of the Query
The XQuery Data Model
Nodes
Node kinds
The node hierarchy
The node family
Roots, documents, and elements
Node identity and name
String and typed values of nodes
Atomic Values
Sequences
Types
Namespaces
3. Expressions: XQuery Building Blocks
Categories of Expressions
Keywords and Names
Whitespace in Queries
Literals
Variables
Function Calls
Comments
Precedence and Parentheses
Comparison Expressions
General Comparisons
General comparisons on multi-item sequences
General comparisons and types
Value Comparisons
Node Comparisons
Conditional (if-then-else) Expressions
Conditional Expressions and Effective Boolean Value
Nesting Conditional Expressions
Switch Expressions
Logical (and/or) Expressions
Precedence of Logical Expressions
Negating a Boolean Value
4. Navigating XML Using Paths
Path Expressions
Path Expressions and Context
Steps and changing context
Steps
Axes
Node Tests
Node name tests
Node name tests and namespaces
Node name tests and wildcards
Node kind tests
Abbreviated Syntax
Other Expressions As Steps
Predicates
Comparisons in Predicates
Using Positions in Predicates
Understanding positional predicates
The position and last functions
Positional predicates and reverse axes
Using Multiple Predicates
More Complex Predicates
A Closer Look at Context
Working with the Context Node
Accessing the Root
Dynamic Paths
The Simple Map Operator
5. Adding Elements and Attributes to Results
Including Elements and Attributes from the Input Document
Direct Element Constructors
Containing Literal Characters
Containing Other Element Constructors
Containing Enclosed Expressions
Enclosed expressions that evaluate to elements
Enclosed expressions that evaluate to attributes
Enclosed expressions that evaluate to atomic values
Enclosed expressions with multiple subexpressions
Specifying Attributes Directly
Declaring Namespaces in Direct Constructors
Use Case: Modifying an Element from the Input Document
Direct Element Constructors and Whitespace
Boundary whitespace
The boundary-space declaration
Forcing boundary whitespace preservation
Computed Constructors
Computed Element Constructors
Names of computed element constructors
Content of computed element constructors
Computed Attribute Constructors
Use Case: Turning Content to Markup
6. Selecting and Joining Using FLWORs
Selecting with Path Expressions
FLWOR Expressions
The for Clause
Range expressions
Multiple for clauses
The let Clause
The where Clause
The return Clause
The Scope of Variables
Quantified Expressions
Binding Multiple Variables
Selecting Distinct Values
Joins
Three-Way Joins
Outer Joins
Outer joins with allowing empty
Joins and Types
7. Sorting and Grouping
Sorting in XQuery
The order by Clause
Using multiple ordering specifications
Sorting and types
Order modifiers
Empty order
Stable ordering
More complex order specifications
The sort Function
Document Order
Document order defined
Sorting in document order
Inadvertent re-sorting in document order
Document Order Comparisons
Reversing the Order
Indicating That Order Is Not Significant
The unordered function
The unordered expression
The ordering mode declaration
Grouping
Grouping Using the group by Clause
Grouping on multiple values
More complex grouping specifications
Aggregating Values
Ignoring “Missing” Values
Counting “Missing” Values
Aggregating on Multiple Values
Constraining and Sorting on Aggregated Values
8. Functions
Built-in Versus User-Defined Functions
Calling Functions
Function Names
Function Signatures
Argument Lists
Argument lists and the empty sequence
Argument lists and sequences
Sequence Types
Calling Functions with the Arrow Operator
User-Defined Functions
Why Define Your Own Functions?
Function Declarations
The Function Body
The Function Name
The Parameter List
Accepting arguments that are nodes versus atomic values
Accepting arguments that are the empty sequence
Functions and Context
Recursive Functions
9. Advanced Queries
Working with Positions and Sequence Numbers
Adding Sequence Numbers to Results
Using the count Clause
Testing for the Last Item
Windowing
Using start and end Conditions
Windows Based on Position
Windows Based on Previous or Next Items
Sliding Windows
Copying Input Elements with Modifications
Adding Attributes to an Element
Removing Attributes from an Element
Removing Attributes from All Descendants
Removing Child Elements
Changing Names
Combining Results
Sequence Constructors
The union Expression
The intersect Expression
The except Expression
Using Intermediate XML Documents
Creating Lookup Tables
Reducing Complexity
10. Namespaces and XQuery
XML Namespaces
Namespace URIs
Declaring Namespaces
Default Namespace Declarations
Namespaces and Attributes
Namespace Declarations and Scope
Namespaces and XQuery
Namespace Declarations in Queries
Predeclared Namespaces
Prolog Namespace Declarations
Default namespace declarations in the prolog
The default function namespace declaration
Other prolog namespace declarations
Namespace Declarations in Direct Element Constructors
Namespace Declarations in Computed Constructors
The Impact and Scope of Namespace Declarations
Scope of namespace declarations
Names affected by namespace declarations
Namespace declarations and input elements
Controlling Namespace Declarations in Your Results
In-Scope Versus Statically Known Namespaces
Controlling the Copying of Namespace Declarations
URI-Qualified Names
11. A Closer Look at Types
The XQuery Type System
Advantages of a Strong Type System
Do You Need to Care About Types?
The Built-in Types
Types, Nodes, and Atomic Values
Nodes and Types
Atomic Values and Types
Type Checking in XQuery
The Static Analysis Phase
The Dynamic Evaluation Phase
Automatic Type Conversions
Subtype Substitution
Type Promotion
Casting of Untyped Values
Atomization
Effective Boolean Value
Function Conversion Rules
Sequence Types
Occurrence Indicators
Generic Sequence Types
Atomic Type Names As Sequence Types
Element and Attribute Tests
Sequence Type Matching
The instance of Expression
Constructors and Casting
Constructors
The Cast Expression
The Castable Expression
Casting Rules
Casting among the primitive types
Casting from xs:string or xs:untypedAtomic
Casting to xs:string or xs:untypedAtomic
Casting among derived types
12. Prologs, Modules and Variables
Structure of a Query: Prolog and Body
Prolog Declarations
The Version Declaration
Assembling Queries from Multiple Modules
Library Modules
Importing a Library Module
Multiple module imports
Multi-level imports
Loading a Library Module Dynamically
Variable Declarations
Variable Declaration Syntax
The Scope of Variables
Variable Names
Initializing Expressions
External Variables
Private Functions and Variables
Declaring External Functions
13. Inputs and Outputs
Types of Input and Output Documents
Accessing Input Documents
Accessing a Single Document with a Function
Resolving URIs of input documents
Accessing a Collection
Setting the Context Outside the Query
Using Variables
Setting the Context in the Prolog
Serializing Output
Serialization Methods
Serialization Parameters
Specifying Serialization Parameters Using Option Declarations
Specifying Serialization Parameters Using a Separate XML Document
Serialization Errors
14. Using Schemas with XQuery
What Is a Schema?
Why Use Schemas with Queries?
W3C XML Schema: A Brief Overview
Element and Attribute Declarations
Types
Simple and complex types
User-defined types
List types
Namespaces and XML Schema
In-Scope Schema Definitions
Where Do In-Scope Schema Definitions Come from?
Schema Imports
Importing a schema with no target namespace
Importing multiple schemas with the same target namespace
Schema imports and library modules
Schema Validation and Type Assignment
The Validate Expression
Validation Mode
Assigning Type Annotations to Nodes
Nodes and Typed Values
Types and Newly Constructed Elements and Attributes
Sequence Types and Schemas
15. Static Typing
What Is Static Typing?
Obvious Static Type Errors
Static Typing and Schemas
Raising “False” Errors
Static Typing Expressions and Constructs
The Typeswitch Expression
The Treat Expression
Type Declarations
Type Declarations in FLWORs
Type Declarations in Quantified Expressions
Type Declarations in Global Variable Declarations
The zero-or-one, one-or-more, and exactly-one Functions
16. Writing Better Queries
Query Design Goals
Clarity
Improving the Layout
Choosing Names
Using Comments for Documentation
Modularity
Robustness
Handling Data Variations
Handling Missing Values
Absent values
Empty and nil values
Default “missing” values
Error Handling
Avoiding Dynamic Errors
The error and trace Functions
Try/catch
Performance
Avoid Reevaluating the Same or Similar Expressions
Avoid Unnecessary Sorting
Avoid Expensive Path Expressions
Use Predicates Instead of where Clauses
17. Working with Numbers
The Numeric Types
The xs:decimal Type
The xs:integer Type
The xs:float and xs:double Types
Constructing Numeric Values
The number Function
Numeric Type Promotion
Comparing Numeric Values
Arithmetic Operations
Arithmetic Operations on Multiple Values
Arithmetic Operations and Types
Precedence of Arithmetic Operators
Addition, Subtraction, and Multiplication
Division
Modulus (Remainder)
Functions on Numbers
Formatting Numbers
Formatting Integers
Formatting Decimal Numbers
The Decimal Format Declaration
18. Working with Strings
The xs:string Type
Constructing Strings
String Literals
The xs:string Constructor and the string Function
Comparing Strings
Comparing Entire Strings
Determining Whether a String Contains Another String
Matching a String to a Pattern
Substrings
Finding the Length of a String
Concatenating and Splitting Strings
Concatenating Strings
Splitting Strings Apart
Converting Between Code Points and Strings
Manipulating Strings
Converting Between Uppercase and Lowercase
Replacing Individual Characters in Strings
Replacing Substrings That Match a Pattern
Whitespace and Strings
Normalizing Whitespace
Internationalization Considerations
Collations
Unicode Normalization
Determining the Language of an Element
19. Regular Expressions
The Structure of a Regular Expression
Atoms
Quantifiers
Parenthesized Sub-Expressions and Branches
Representing Individual Characters
Representing Any Character
Representing Groups of Characters
Multi-Character Escapes
Category Escapes
Block Escapes
Character Class Expressions
Single Characters and Ranges
Subtraction from a Range
Negative Character Class Expressions
Escaping Rules for Character Class Expressions
Reluctant Quantifiers
Anchors
Anchors and Multi-Line Mode
Back-References
Using Flags
Using Sub-Expressions with Replacement Variables
20. Working with Dates, Times, and Durations
The Date and Time Types
Constructing and Casting Dates and Times
Time Zones
Explicit versus implicit time zones
Adjusting time zones
Finding the time zone of a value
Comparing Dates and Times
The Duration Types
The xs:yearMonthDuration and xs:dayTimeDuration Types
Comparing Durations
Extracting Components of Dates, Times, and Durations
Formatting Dates and Times
Using Arithmetic Operators on Dates, Times, and Durations
Subtracting Dates and Times
Adding and Subtracting Durations from Dates and Times
Adding and Subtracting Two Durations
Multiplying and Dividing Durations by Numbers
Dividing Durations by Durations
The Date Component Types
21. Working with Qualified Names, URIs, and IDs
Working with Qualified Names
Retrieving Node Names
Constructing Qualified Names
Other Name-Related Functions
Working with URIs
Base and Relative URIs
Using the xml:base attribute
Finding the base URI of a node
Resolving URIs
Static base URI
Documents and URIs
Finding the URI of a document
Opening a document from a dynamic value
Escaping URIs
Working with IDs
Joining IDs and IDREFs
Constructing ID Attributes
Generating Unique ID Values
22. Working with Other XML Constructs
XML Comments
XML Comments and the Data Model
Querying Comments
Comments and Sequence Types
Constructing Comments
Processing Instructions
Processing Instructions and the Data Model
Querying Processing Instructions
Processing Instructions and Sequence Types
Constructing Processing Instructions
Documents
Document Nodes and the Data Model
Document Nodes and Sequence Types
Constructing Document Nodes
Text Nodes
Text Nodes and the Data Model
Querying Text Nodes
Text Nodes and Sequence Types
Why Work with Text Nodes?
Constructing Text Nodes
XML Entity and Character References
CDATA Sections
23. Function Items and Higher-Order Functions
Constructing functions and calling them dynamically
Named Function References
Using function-lookup to Obtain a Function
Inline Function Expressions
Partial Function Application
Syntax recap
Functions and sequence types
Higher-Order Functions
Built-In Higher Order Functions
Writing Your Own Higher Order Functions
24. Maps, Arrays and JSON
Maps
Constructing Maps
Using a map constructor
Using map:entry and map:merge to create maps
Looking up Map Values
Using the map:get function
Invoking maps as functions
Using the lookup operator on maps
Querying Maps
Changing Maps
Iterating over Entries in a Map
Maps and Sequence Types
Arrays
Constructing Arrays
Arrays vs. Sequences
Looking up Array Values
Using the array:get function
Invoking arrays as functions
Using the lookup operator on arrays
Querying Arrays
Changing Arrays
Arrays and Sequence Types
JSON
Parsing JSON
Serializing JSON
Converting Between JSON and XML
25. Additional XQuery-Related Standards
XQuery Update Facility
Full-Text Search
XQueryX
XQuery API for Java (XQJ)
26. Implementation-Specific Features
Conformance
Version Support
Setting the Query Context
The Option Declaration
Extension Expressions
Annotations
27. XQuery for SQL Users
Relational Versus XML Data Models
Comparing SQL Syntax with XQuery Syntax
A Simple Query
Conditions and Operators
Comparisons
Arithmetic and string operators
Boolean operators
Functions
Selecting Distinct Values
Working with Multiple Tables and Subqueries
Subselects
Combining queries using set operators
Grouping
Combining SQL and XQuery
Combining Structured and Semi-structured Data
Flexible Data Structures
SQL/XML
28. XQuery for XSLT Users
XQuery and XPath
XQuery Versus XSLT
Shared Components
Equivalent Components
Differences
Paradigm differences: push versus pull
Optimization for particular use cases
Convenient features of XSLT
XQuery Backward Compatibility with XPath 1.0
Data Model
New Expressions
Path Expressions
Function Conversion Rules
Arithmetic and Comparison Expressions
Built-in Functions
A. Built-in Function Reference
abs
math:acos
adjust-date-to-timezone
adjust-dateTime-to-timezone
adjust-time-to-timezone
analyze-string
array:append
apply
math:asin
math:atan
math:atan2
available-environment-variables
avg
base-uri
boolean
ceiling
codepoint-equal
codepoints-to-string
collation-key
collection
compare
concat
contains
map:contains
contains-token
math:cos
count
current-date
current-dateTime
current-time
data
dateTime
day-from-date
day-from-dateTime
days-from-duration
deep-equal
default-collation
distinct-values
doc
doc-available
document-uri
element-with-id
empty
encode-for-uri
ends-with
map:entry
environment-variable
error
escape-html-uri
exactly-one
exists
math:exp
math:exp10
false
filter
array:filter
array:flatten
floor
fold-left
array:fold-left
fold-right
array:fold-right
for-each
array:for-each
map:for-each
for-each-pair
array:for-each-pair
format-date
format-dateTime
format-integer
format-number
format-time
function-arity
function-lookup
function-name
generate-id
array:get
map:get
has-children
head
array:head
hours-from-dateTime
hours-from-duration
hours-from-time
id
idref
implicit-timezone
in-scope-prefixes
index-of
innermost
insert-before
array:insert-before
iri-to-uri
array:join
json-doc
map:keys
lang
last
load-xquery-module
local-name
local-name-from-QName
math:log
math:log10
lower-case
matches
max
map:merge
min
minutes-from-dateTime
minutes-from-duration
minutes-from-time
month-from-date
month-from-dateTime
months-from-duration
name
namespace-uri
namespace-uri-for-prefix
namespace-uri-from-QName
nilled
node-name
normalize-space
normalize-unicode
not
number
one-or-more
outermost
parse-ietf-date
parse-json
parse-xml
parse-xml-fragment
path
math:pi
position
math:pow
prefix-from-QName
map:put
QName
random-number-generator
remove
array:remove
map:remove
replace
resolve-QName
resolve-uri
reverse
array:reverse
root
round
round-half-to-even
seconds-from-dateTime
seconds-from-duration
seconds-from-time
serialize
math:sin
array:size
map:size
sort
array:sort
math:sqrt
starts-with
static-base-uri
string
string-join
string-length
string-to-codepoints
array:subarray
subsequence
substring
substring-after
substring-before
sum
tail
array:tail
math:tan
timezone-from-date
timezone-from-dateTime
timezone-from-time
tokenize
trace
transform
translate
true
unordered
unparsed-text
unparsed-text-available
unparsed-text-lines
upper-case
uri-collection
year-from-date
year-from-dateTime
years-from-duration
zero-or-one
B. Built-in Types
xs:anyAtomicType
xs:anySimpleType
xs:anyType
xs:anyURI
Casting and Comparing xs:anyURI Values
xs:base64Binary
xs:boolean
Constructing xs:boolean Values
Casting xs:boolean Values
xs:byte
xs:date
xs:dateTime
xs:dateTimeStamp
xs:dayTimeDuration
xs:decimal
Casting xs:decimal Values
xs:double
Casting xs:double Values
xs:duration
xs:ENTITIES
xs:ENTITY
xs:error
xs:float
Casting xs:float Values
xs:gDay
xs:gMonth
xs:gMonthDay
xs:gYear
xs:gYearMonth
xs:hexBinary
xs:ID
xs:IDREF
xs:IDREFS
xs:int
xs:integer
Casting xs:integer Values
xs:language
xs:long
xs:Name
xs:NCName
xs:negativeInteger
xs:NMTOKEN
xs:NMTOKENS
xs:nonNegativeInteger
xs:nonPositiveInteger
xs:normalizedString
xs:NOTATION
xs:numeric
xs:positiveInteger
xs:QName
xs:short
xs:string
xs:time
xs:token
xs:unsignedByte
xs:unsignedInt
xs:unsignedLong
xs:unsignedShort
xs:untyped
xs:untypedAtomic
xs:yearMonthDuration
C. Error Summary
FOAP0001
FOAR0001
FOAR0002
FOAY0001
FOAY0002
FOCA0001
FOCA0002
FOCA0003
FOCA0005
FOCA0006
FOCH0001
FOCH0002
FOCH0003
FOCH0004
FODC0001
FODC0002
FODC0003
FODC0004
FODC0005
FODC0006
FODC0010
FODF1280
FODF1310
FODT0001
FODT0002
FODT0003
FOER0000
FOFD1340
FOFD1350
FOJS0001
FOJS0003
FOJS0004
FOJS0005
FOJS0006
FOJS0007
FOMP0001
FONS0004
FONS0005
FOQM0001
FOQM0002
FOQM0003
FOQM0004
FOQM0005
FORG0001
FORG0002
FORG0003
FORG0004
FORG0005
FORG0006
FORG0008
FORG0009
FORG0010
FORX0001
FORX0002
FORX0003
FORX0004
FOTY0012
FOTY0013
FOTY0014
FOTY0015
FOUT1170
FOUT1190
FOUT1200
FOXT0001
FOXT0002
FOXT0003
FOXT0004
SENR0001
SEPM0004
SEPM0009
SEPM0010
SEPM0016
SEPM0017
SEPM0018
SEPM0019
SERE0003
SERE0005
SERE0006
SERE0008
SERE0012
SERE0014
SERE0015
SERE0020
SERE0021
SERE0022
SERE0023
SESU0007
SESU0011
SESU0013
XPDY0002
XPDY0050
XPDY0130
XPDY0139
XPST0001
XPST0003
XPST0005
XPST0008
XPST0017
XPST0051
XPST0080
XPST0081
XPTY0004
XPTY0018
XPTY0019
XPTY0020
XPTY0117
XQDY0025
XQDY0026
XQDY0027
XQDY0041
XQDY0044
XQDY0054
XQDY0061
XQDY0064
XQDY0072
XQDY0074
XQDY0084
XQDY0091
XQDY0092
XQDY0096
XQDY0101
XQDY0102
XQDY0137
XQST0009
XQST0012
XQST0013
XQST0016
XQST0022
XQST0031
XQST0032
XQST0033
XQST0034
XQST0035
XQST0038
XQST0039
XQST0040
XQST0045
XQST0046
XQST0047
XQST0048
XQST0049
XQST0052
XQST0055
XQST0057
XQST0058
XQST0059
XQST0060
XQST0065
XQST0066
XQST0067
XQST0068
XQST0069
XQST0070
XQST0071
XQST0075
XQST0076
XQST0079
XQST0085
XQST0087
XQST0088
XQST0089
XQST0090
XQST0094
XQST0097
XQST0098
XQST0099
XQST0103
XQST0104
XQST0106
XQST0108
XQST0109
XQST0110
XQST0111
XQST0113
XQST0114
XQST0115
XQST0116
XQST0118
XQST0119
XQST0125
XQST0129
XQST0134
XQTY0024
XQTY0030
XQTY0086
XQTY0105
Index
← Prev
Back
Next →
← Prev
Back
Next →