Contents Overview 1 Lab 8: Using Simple and Advanced Review 22 Module 8: Using XQuery to Link and Query XML Documents... iv Module 8: Using XQuery to Link and Query XML Documents Mate
Trang 1Contents
Overview 1
Lab 8: Using Simple and Advanced
Review 22
Module 8: Using XQuery to Link and Query XML Documents
Trang 2Information in this document, including URL and other Internet Web site references, is subject to change without notice Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, places or events is intended or should be inferred Complying with all applicable copyright laws is the responsibility of the user Without limiting the rights under copyright, no part
of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted
in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property
2001 Microsoft Corporation All rights reserved
Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BackOffice, bCentral, BizTalk, FrontPage, MSDN, MSN, Netshow, PowerPoint, SharePoint, Visio, Visual Basic, Visual C++, Visual C#, Visual InterDev, Visual Studio, Windows Media, and Xbox are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A and/or other countries
The names of actual companies and products mentioned herein may be the trademarks of their respective owners
Trang 3Instructor Notes
This module is designed to provide participants with an introduction to the core concepts of XQuery and to familiarize them with the structure of XQuery expressions
XQuery can be a challenging topic to teach There is only enough time to cover what it is, what it can do, and its basic syntax, but not in any depth This module leaves the detailed coverage of XQuery for later courses Just as Course
1913A, Exchanging and Transforming Data Using XML and XSLT, is a
five-day course on Extensible Stylesheet Language Transformations (XSLT), it is possible to devote a full five-day course to XQuery after it is fully specified and achieves W3C Recommendation status
Stress that XQuery is still in development by the World Wide Web Consortium (W3C) and is not yet finalized Participants may ask why the module is
included, considering the state of development of XQuery It is included, because XQuery is expected to be a W3C Recommendation by the end of 2001 and heavily used in the Internet and environments like Microsoft® NET when it
is a stable Recommendation
XQuery is important, because to effectively do data-centric work on the Internet, you need to have XQuery or its equivalent Without such a language and set of capabilities, the use of XML is severely limited Because XML is
fundamentally about representing data and metadata, XQuery is essential
After completing this module, participants will be able to:
! Understand the key concepts behind XQuery
! Design simple and advanced XQuery expressions
! Identify solutions where XQuery is the suitable technology
! Describe how you might apply XQuery expressions to query an XML document
! Relate XQuery to the broader Microsoft NET Framework and strategy
Presentation:
50 Minutes
Lab:
20 Minutes
Trang 4iv Module 8: Using XQuery to Link and Query XML Documents
Materials and Preparation
To teach this module, you need the following materials:
! Microsoft PowerPoint® file 2500A_08.ppt
! Access to the Microsoft MSDN® XQuery demonstration site at http://131.107.228.20/xquerydemo/
! Access to the W3C XQuery site at http://www.w3.org/XML/Query Download and read the most recent W3C and MSDN Online documentation
on XQuery
To prepare for this module:
! Read all of the materials for this module
! Complete the lab
! Deliver the demonstration that uses the XQuery demonstration site
This site is likely to be modified from time to time to reflect W3C progress and developments, so check it regularly so that you are not surprised by changes It is best to review it before each class
! Review as many XQuery use cases as you can to consolidate your understanding of XQuery
! Work out additional examples of where XQuery can be used in your location and with your clients
Required Materials
Preparation Tasks
Trang 5Instructor Setup for the Lab
For this lab, the instructor computer and all student computers must be connected to the Internet The lab enables participants to use the MSDN XQuery demonstration site to gain more familiarity with the capabilities of XQuery Become thoroughly familiar with the demonstration site and be prepared to answer questions
! Prepare for the lab
1 Download and review the data files from the XQuery demonstration site These are for ease of reference only
2 Review the lab steps and be prepared to explain the XQuery syntax if required Participants are likely to find some of the queries, particularly the advanced queries, somewhat confusing
Demonstration
There are only two demonstrations in this module The first demonstration gives a quick example of how easy it is to do an SQL query with a good graphical tool The discussion point of the demonstration is to contrast this with the tedious coding that would be required for XQuery without such tools You may want to keep this demonstration short or you may want to discuss it in some detail, depending on the backgrounds of the participants Be sure to point out that at present there is no comparable graphical tool for use with XQuery
! Prepare for the demonstration
1 Be sure that you are familiar with building queries with both the Microsoft SQL Server™ and Microsoft Access graphical interfaces
2 For the demonstration of the graphical interface in SQL Server, complete the following steps:
a Open Microsoft SQL Server Enterprise Manager
b Expand the pubs database under Databases
c Under the pubs database, click Tables
d Right-click the publishers table, point to Open Table, and then click Query
e Click the Add table toolbar button, and then add the titles, titleauthor, and authors tables
f Choose the following columns: pub_name, title, price, au_lname, and
au_fname Remove the * from the SELECT statement so that you only
have the indicated columns listed in the SELECT statement
g Run the query by clicking the Run icon
h In the criteria cell for pub_name, type New Moon Books and then rerun
Trang 6vi Module 8: Using XQuery to Link and Query XML Documents
3 (Optional) If you have time, repeat the demonstration, this time using Access
a Using Access, open the pubs.mdb file located in the install_folder\
Democode\Mod08 folder
b Maximize the Access window and the database container window
c In the database objects list, click Tables and then click the publishers
table
d On the toolbar, click New Object, and then click Query
e Choose Design View from the list
f On the toolbar, click Show Table to add the titles, titleauthor, and
authors tables
The tables show the relationship links that have been defined between the primary and foreign keys
g Drag the pub_name, title, price, au_lname, and au_fname columns
from the tables into the first row of the query grid, and then click the Run icon
h Show that you get the same results as you did when you used SQL Server
i Switch back to Design view, and in the criteria cell for pub_name, type
New Moon Books
j Rerun the query
k Now select the SQL View of the query, and then compare it with the
SQL query
The second demonstration introduces the participants to the MSDN XQuery language demonstration site so that they will not have to spend much time becoming familiar with it during the lab
! Prepare for the demonstration
1 On the MSDN XQuery demonstration site, open each of the links in the left pane and be sure that you are familiar with the topics presented there
2 Become familiar with the behavior of the Web page
You will need to refresh the page or use the Back button to return to the
XQuery page after you select links for background topics (for example, Readme, Known issues) The page also refreshes when you select a sample query
3 Show the class the sample files that have been copied to the
install_folder\Democode\Mod08 folder
4 Open bib.xml to show the structure of the document
5 Under Default Cases, click Example 2, and then run the query
You may have to scroll to see all parts of the query page
6 Discuss the results
7 Repeat this sequence for Example 1 and for Example 4 and walk through each query and the results
Using a Variety of
XQuery Methods
Trang 7Module Strategy
The module should not take longer than the allotted time to do Because this is likely to be new material to most participants, emphasize that this is an introduction to an aspect of XML technologies that is going to be very important to XML and to Microsoft NET Use the following strategy to present this module:
Stress that XQuery is incomplete, as long as that remains true When XQuery reaches W3C Recommendation status, you will need to adjust the presentation of the module if the course is not revised Be sure to explain FLWR expressions These will be seen in the examples in the second lesson and in the lab
Work through each of the topic slides and discuss the central concepts and associated ideas The class may want to discuss some topics in more depth Keep an eye on the time
Be sure that you are comfortable with the complexities of the XQuery examples The best way to prepare for this is to read the W3C papers carefully and to spend adequate time with the XQuery demonstration site to get a good idea of how these queries look and behave
! Using XQuery This lesson is designed to focus attention on examples of XQuery syntax, which you can work through in detail with the class
Be ready to expand upon these examples with what you learned from the W3C documents and the XQuery demonstration site Focus on the basics and try not to discuss the very advanced features of XQuery, because they will likely be too difficult for newcomers to the topic
Discuss the similarities and differences between SQL queries and XML queries Emphasize the differences between SQL and XML in their ability
to handle redefinition of output Also emphasize that XML is self-defining and self-modifying in this regard
Trang 9Overview
! What Is XQuery?
! Using XQuery
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
In this module, you will see why Extensible Markup Language (XML) requires
a specific query language and how XQuery fulfills this requirement
This module is not designed to give you an exhaustive introduction to XQuery, because there is neither time nor space for that in this course It is designed to provide you with an introduction to XQuery and the background of significant developments in XQuery
This module shows the difference between XQuery and XML Path Language (XPath) It then shows examples of XQuery expressions and syntax with sample XML documents to show how XQuery enables you to retrieve data in a manner analogous to an SQL query in a relational database
After completing this module, you will be able to:
! Understand the key concepts behind XQuery
! Design simple and advanced XQuery expressions
! Identify solutions where XQuery is the suitable technology
! Describe how you might apply XQuery expressions to query an XML document
! Relate XQuery to the broader Microsoft® NET Framework and strategy
Introduction
Objectives
Trang 102 Module 8: Using XQuery to Link and Query XML Documents
Lesson: What Is XQuery?
! Why Do You Need XQuery?
! XQuery Expression Basics
! XQuery and SQL
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
In this lesson the rationale behind the development of XQuery is explained You will learn how XQuery and XPath are related and how XQuery and SQL are related You will gain a first exposure to the query syntax of XQuery After completing this lesson you will be able to:
! Explain the relationship between XQuery and XPath
! Interpret the syntax of an XQuery expression
! Describe the uses of XQuery in relation to XPath and SQL
Lesson Objectives
Trang 11Why Do You Need XQuery?
XPath is limited:
! Navigate a single XML source
at a time
! Query XML sources only
! Run simple queries
With XQuery, you can:
! Query multiple sources
at once
! Simultaneously query XML sources and relational data
! Run complex queries
XML Example XQuery Expression SQL Query
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
Every day more data is made available formatted in XML Still much of the world’s data is stored in relational databases There is a need for an efficient query language that can extend a query across a variety of XML sources and to relational data sources The World Wide Web Consortium (W3C) has been working on a specification to address this need The specification that has emerged has sometimes been called XML Query but is more commonly known simply as XQuery
For more information about XML Query, see the XML Query page at http://www.w3.org/XML/Query
XPath is inadequate to extend queries across multiple XML sources You cannot use XPath to query relational data You can only use XPath to and this has helped to drive the efforts to find a better query language As XQuery is developed, both the XPath and Extensible Stylesheet Language (XSL) working groups are engaged in revisions to their respective technologies to take account
of the power and functionality of XQuery
As currently conceived by W3C, XQuery will ultimately be able to work across any number of data stores, including relational, structured and semistructured documents, and object repositories
There are numerous scenarios that require a query of XML data Here are two brief examples:
! Several different business partners have sent you their catalogues in XML, and you want to compare prices and features of like products
! A scientist is researching medical uses of botanicals and wants to compare her research results with colleagues in other institutions Research data is stored in both XML data stores and relational databases
Trang 124 Module 8: Using XQuery to Link and Query XML Documents
The ideas incorporated in XQuery for powerful querying of data documents have led to revisions of the XPath specifications Indeed, the W3C specification XPath version 2.0 is being developed jointly by the XPath and XQuery working groups When you consider the basic issues involved in navigating in XML and querying data in XML, it is clear why the technologies are being developed together
XPath has limited support for data types, and it is not rich enough for querying data For example, you cannot use XPath for relational comparisons, sorting or modifying XML content In contrast, XQuery enables you to:
! List all parts produced by a manufacturer that have a price that is less than a particular value
! List all parts produced by a manufacturer and then group them by category inside an output element
! Find all parts of the same type produced by Manufacturer A and Manufacturer B and then produce a combined listing
The development of XQuery will lead to the enhancement of the XPath language after the working groups finish their work on XPath 2.0 According to W3C, XQuery must be able to handle the following:
! Data and data types
For a full listing of the requirements for XQuery, see the topic “XQuery
Semantics by Example” in the W3C document XQuery 1.0 Formal Semantics
XQuery and XPath 2.0
XQuery Capabilities
Note
Trang 13Consider a situation in which you have an XML data document named books.xml This document has an associated document type definition (DTD)
or XML Schema Definition (XSD) Sample data from books.xml (from the
Pubs database from Microsoft SQL Server™ 2000), located at http://contoso.msft, looks like this:
<title>You Can Combat Computer Stress!</title>
<publisher>New Moon Books</publisher>
<title>Is Anger the Enemy?</title>
<publisher>New Moon Books</publisher>
<book pubyear={ $b/@pubyear }>
{ $b/title } </book>
}
</books>
Example
Trang 146 Module 8: Using XQuery to Link and Query XML Documents
The following is the same query in SQL:
SELECT pubyear, title FROM books
WHERE (pubyear > 1990) AND (publisher = 'New Moon Books')?) From the XQuery expression, you get:
Trang 15XQuery Expression Basics
! XQuery expressions use the FLWR syntax
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
In the same way that SQL was developed to query relational data, XQuery is designed to query XML documents and data sources Importantly, it is also a relational query language that builds on the history of SQL and other
predicate-based systems and algebra XQuery is strongly typed and a strong predicate language By using FLWR expressions in XQuery expressions, you can:
! Select data and metadata from data documents or other data stores
! Transform data and metadata by returning query results and manipulate the results into a different XML tree form upon retrieval
! Query document fragments that do not have a root element
! Access and manipulate data of all types, including user-defined data types This presumes full support for the XSD data types specified by W3C
Introduction
Trang 168 Module 8: Using XQuery to Link and Query XML Documents
The central ideas of XQuery are analogous to SQL query concepts FLWR
(pronounced like flower) expressions allow queries of XML data documents
and fragments of documents that are similar to SQL queries
An XQuery expression can be a simple XPath expression, but users and application usually require more sophisticated queries XQuery expressions are built upon the following four keywords Keywords in XQuery are not
case-sensitive
! FOR You use the FOR clause to iterate through data by using one or more variables that are associated with expressions The expressions can be path references or other legitimate XML expressions
! LET You also use the LET clause to bind variables However, unlike the FOR clause, it does not iterate Therefore, it binds a variable to a single value for the expression
! WHERE The WHERE clause is similar to the WHERE clause in SQL It filters the data to determine which data qualifies as a result of the query
! RETURN The RETURN clause is what generates the output from the FLWR expression and can be values or a node pattern It can be further qualified by element constructors, sorting, calculations, or comparisons An element constructor is an XML element that represents itself and allows you to generate new elements from within the query
FLWR expressions are processed in a particular order Any FOR or LET clause
is processed first, then, any WHERE clause is processed, and then any RETURN clause is processed
FLWR expressions