ebook img

XQuery: Search Across a Variety of XML Data PDF

736 Pages·2015·8.15 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview XQuery: Search Across a Variety of XML Data

SECOND EDITION XQuery Priscilla Walmsley Boston XQuery by Priscilla Walmsley Copyright © 2015 Priscilla Walmsley. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( http://safaribooksonline.com ). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected] . Editor: Meg Foley Proofreader: FILL IN PROOFREADER Production Editor: FILL IN PRODUCTION EDI‐ Indexer: FILL IN INDEXER TOR Interior Designer: David Futato Copyeditor: FILL IN COPYEDITOR Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest March 2015: Second Edition Revision History for the Second Edition 2015-06-25: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491915103 for release details. While the publisher and the author(s) have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibil‐ ity for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-91510-3 [FILL IN] Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii 1. Introduction to XQuery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 What Is XQuery? 23 Capabilities of XQuery 23 Uses for XQuery 24 Processing Scenarios 24 Easing into XQuery 25 Path Expressions 27 FLWORs 29 Adding XML Elements and Attributes 30 Adding Elements 30 Adding Attributes 32 Functions 33 Joins 33 Aggregating and Grouping Values 34 2. XQuery Foundations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 The Design of the XQuery Language 35 XQuery in Context 36 XQuery and XPath 36 XQuery Versus XSLT 36 XQuery Versus SQL 37 XQuery and XML Schema 37 Processing Queries 38 Input Documents 38 The Query 39 The Context 40 iii The Query Processor 40 The Results of the Query 40 The XQuery Data Model 41 Nodes 42 Atomic Values 46 Sequences 47 Types 48 Namespaces 48 3. Expressions: XQuery Building Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Categories of Expressions 51 Keywords and Names 52 Whitespace in Queries 53 Literals 53 Variables 54 Function Calls 54 Comments 55 Precedence and Parentheses 55 Comparison Expressions 57 General Comparisons 57 Value Comparisons 58 Node Comparisons 60 Conditional (if-then-else) Expressions 61 Conditional Expressions and Effective Boolean Value 62 Nesting Conditional Expressions 63 Switch Expressions 63 Logical (and/or) Expressions 65 Precedence of Logical Expressions 65 Negating a Boolean Value 66 4. Navigating XML Using Paths. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Path Expressions 69 Path Expressions and Context 70 Steps 71 Axes 71 Node Tests 72 Abbreviated Syntax 75 Other Expressions As Steps 76 Predicates 77 Comparisons in Predicates 78 Using Positions in Predicates 78 Using Multiple Predicates 81 iv | Table of Contents More Complex Predicates 82 A Closer Look at Context 83 Working with the Context Node 83 Accessing the Root 84 Dynamic Paths 85 The Simple Map Operator 85 5. Adding Elements and Attributes to Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Including Elements and Attributes from the Input Document 87 Direct Element Constructors 88 Containing Literal Characters 89 Containing Other Element Constructors 90 Containing Enclosed Expressions 90 Specifying Attributes Directly 93 Declaring Namespaces in Direct Constructors 94 Use Case: Modifying an Element from the Input Document 95 Direct Element Constructors and Whitespace 96 Computed Constructors 99 Computed Element Constructors 99 Computed Attribute Constructors 102 Use Case: Turning Content to Markup 102 6. Selecting and Joining Using FLWORs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Selecting with Path Expressions 105 FLWOR Expressions 105 The for Clause 108 The let Clause 110 The where Clause 112 The return Clause 113 The Scope of Variables 113 Quantified Expressions 114 Binding Multiple Variables 115 Selecting Distinct Values 116 Joins 117 Three-Way Joins 118 Outer Joins 119 Joins and Types 120 7. Sorting and Grouping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Sorting in XQuery 121 The order by Clause 121 The sort Function 125 Table of Contents | v Document Order 125 Document Order Comparisons 127 Reversing the Order 128 Indicating That Order Is Not Significant 128 Grouping 130 Grouping Using the group by Clause 131 Aggregating Values 134 Ignoring “Missing” Values 136 Counting “Missing” Values 136 Aggregating on Multiple Values 137 Constraining and Sorting on Aggregated Values 138 8. Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Built-in Versus User-Defined Functions 141 Calling Functions 141 Function Names 142 Function Signatures 143 Argument Lists 143 Sequence Types 145 Calling Functions with the Arrow Operator 146 User-Defined Functions 146 Why Define Your Own Functions? 146 Function Declarations 147 The Function Body 148 The Function Name 149 The Parameter List 149 Functions and Context 152 Recursive Functions 152 9. Advanced Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Working with Positions and Sequence Numbers 155 Adding Sequence Numbers to Results 155 Using the count Clause 157 Testing for the Last Item 159 Windowing 160 Using start and end Conditions 162 Windows Based on Position 163 Windows Based on Previous or Next Items 164 Sliding Windows 166 Copying Input Elements with Modifications 166 Adding Attributes to an Element 167 Removing Attributes from an Element 168 vi | Table of Contents Removing Attributes from All Descendants 169 Removing Child Elements 169 Changing Names 170 Combining Results 172 Sequence Constructors 172 The union Expression 172 The intersect Expression 173 The except Expression 173 Using Intermediate XML Documents 174 Creating Lookup Tables 174 Reducing Complexity 175 10. Namespaces and XQuery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 XML Namespaces 177 Namespace URIs 177 Declaring Namespaces 178 Default Namespace Declarations 179 Namespaces and Attributes 180 Namespace Declarations and Scope 180 Namespaces and XQuery 181 Namespace Declarations in Queries 182 Predeclared Namespaces 182 Prolog Namespace Declarations 183 Namespace Declarations in Direct Element Constructors 186 Namespace Declarations in Computed Constructors 187 The Impact and Scope of Namespace Declarations 188 Controlling Namespace Declarations in Your Results 190 In-Scope Versus Statically Known Namespaces 190 Controlling the Copying of Namespace Declarations 193 URI-Qualified Names 197 11. A Closer Look at Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 The XQuery Type System 199 Advantages of a Strong Type System 199 Do You Need to Care About Types? 200 The Built-in Types 201 Types, Nodes, and Atomic Values 203 Nodes and Types 203 Atomic Values and Types 204 Type Checking in XQuery 204 The Static Analysis Phase 205 The Dynamic Evaluation Phase 205 Table of Contents | vii Automatic Type Conversions 205 Subtype Substitution 206 Type Promotion 206 Casting of Untyped Values 206 Atomization 206 Effective Boolean Value 207 Function Conversion Rules 209 Sequence Types 210 Occurrence Indicators 211 Generic Sequence Types 212 Atomic Type Names As Sequence Types 213 Element and Attribute Tests 213 Sequence Type Matching 214 The instance of Expression 214 Constructors and Casting 215 Constructors 215 The Cast Expression 216 The Castable Expression 217 Casting Rules 217 12. Prologs, Modules and Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Structure of a Query: Prolog and Body 221 Prolog Declarations 222 The Version Declaration 223 Assembling Queries from Multiple Modules 224 Library Modules 225 Importing a Library Module 225 Loading a Library Module Dynamically 227 Variable Declarations 228 Variable Declaration Syntax 228 The Scope of Variables 229 Variable Names 229 Initializing Expressions 229 External Variables 230 Private Functions and Variables 230 Declaring External Functions 231 13. Inputs and Outputs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Types of Input and Output Documents 233 Accessing Input Documents 234 Accessing a Single Document with a Function 234 Accessing a Collection 235 viii | Table of Contents Setting the Context Outside the Query 236 Using Variables 236 Setting the Context in the Prolog 237 Serializing Output 237 Serialization Methods 238 Serialization Parameters 240 Specifying Serialization Parameters Using Option Declarations 244 Specifying Serialization Parameters Using a Separate XML Document 245 Serialization Errors 245 14. Using Schemas with XQuery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 What Is a Schema? 247 Why Use Schemas with Queries? 248 W3C XML Schema: A Brief Overview 249 Element and Attribute Declarations 250 Types 250 Namespaces and XML Schema 252 In-Scope Schema Definitions 253 Where Do In-Scope Schema Definitions Come from? 253 Schema Imports 253 Schema Validation and Type Assignment 255 The Validate Expression 256 Validation Mode 257 Assigning Type Annotations to Nodes 258 Nodes and Typed Values 259 Types and Newly Constructed Elements and Attributes 260 Sequence Types and Schemas 261 15. Static Typing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 What Is Static Typing? 265 Obvious Static Type Errors 266 Static Typing and Schemas 266 Raising “False” Errors 267 Static Typing Expressions and Constructs 267 The Typeswitch Expression 268 The Treat Expression 270 Type Declarations 271 Type Declarations in FLWORs 271 Type Declarations in Quantified Expressions 272 Type Declarations in Global Variable Declarations 272 The zero-or-one, one-or-more, and exactly-one Functions 273 Table of Contents | ix

Description:
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.0, this in-depth tutorial not only walks you through the XQuery specification, but also teaches you how to program with this hig
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.