- Sams Teach Yourself XML in 21 Days, Third Edition
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- All About Markup Languages
- All About XML
- Looking at XML in a Browser
- Working with XML Data Yourself
- Structuring Your Data
- Creating Well-Formed XML Documents
- Creating Valid XML Documents
- How XML Is Used in the Real World
- Online XML Resources
- Summary
- Q&A
- Workshop
- Day 2. Creating XML Documents
- Choosing an XML Editor
- Using XML Browsers
- Using XML Validators
- Creating XML Documents Piece by Piece
- Creating Prologs
- Creating an XML Declaration
- Creating XML Comments
- Creating Processing Instructions
- Creating Tags and Elements
- Creating CDATA Sections
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 3. Creating Well-Formed XML Documents
- What Makes an XML Document Well-Formed?
- Creating an Example XML Document
- Understanding the Well-Formedness Constraints
- Using XML Namespaces
- Understanding XML Infosets
- Understanding Canonical XML
- Summary
- Q&A
- Workshop
- Day 4. Creating Valid XML Documents: DTDs
- All About DTDs
- Validating a Document by Using a DTD
- Creating Element Content Models
- Commenting a DTD
- Supporting External DTDs
- Handling Namespaces in DTDs
- Summary
- Q&A
- Workshop
- Declaring Attributes in DTDs
- Day 5. Handling Attributes and Entities in DTDs
- Specifying Default Values
- Specifying Attribute Types
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 6. Creating Valid XML Documents: XML Schemas
- Using XML Schema Tools
- Creating XML Schemas
- Dissecting an XML Schema
- The Built-in XML Schema Elements
- Creating Elements and Types
- Specifying a Number of Elements
- Specifying Element Default Values
- Creating Attributes
- Summary
- Q&A
- Workshop
- Day 7. Creating Types in XML Schemas
- Restricting Simple Types by Using XML Schema Facets
- Creating XML Schema Choices
- Using Anonymous Type Definitions
- Declaring Empty Elements
- Declaring Mixed-Content Elements
- Grouping Elements Together
- Grouping Attributes Together
- Declaring all Groups
- Handling Namespaces in Schemas
- Annotating an XML Schema
- Summary
- Q&A
- Workshop
- Part I. In Review
- Well-Formed Documents
- Valid Documents
- Part II: At a Glance
- Day 8. Formatting XML by Using Cascading Style Sheets
- Our Sample XML Document
- Introducing CSS
- Connecting CSS Style Sheets and XML Documents
- Creating Style Sheet Selectors
- Using Inline Styles
- Creating Style Rule Specifications in Style Sheets
- Summary
- Q&A
- Workshop
- Day 9. Formatting XML by Using XSLT
- Introducing XSLT
- Transforming XML by Using XSLT
- Writing XSLT Style Sheets
- Using <xsl:apply-templates>
- Using <xsl:value-of> and <xsl:for-each>
- Matching Nodes by Using the match Attribute
- Working with the select Attribute and XPath
- Using <xsl:copy>
- Using <xsl:if>
- Using <xsl:choose>
- Specifying the Output Document Type
- Summary
- Q&A
- Workshop
- Day 10. Working with XSL Formatting Objects
- Introducing XSL-FO
- Using XSL-FO
- Using XSL Formatting Objects and Properties
- Building an XSL-FO Document
- Handling Inline Formatting
- Formatting Lists
- Formatting Tables
- Summary
- Q&A
- Workshop
- Part II. In Review
- Using CSS
- Using XSLT
- Using XSL-FO
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Why XHTML?
- Writing XHTML Documents
- Validating XHTML Documents
- The Basic XHTML Elements
- Organizing Text
- Formatting Text
- Selecting Fonts: <font>
- Comments: <!-->
- Summary
- Q&A
- Workshop
- Day 12. Putting XHTML to Work
- Creating Hyperlinks: <a>
- Linking to Other Documents: <link>
- Handling Images: <img>
- Creating Frame Documents: <frameset>
- Creating Frames: <frame>
- Creating Embedded Style Sheets: <style>
- Formatting Tables: <table>
- Creating Table Rows: <tr>
- Formatting Table Headers: <th>
- Formatting Table Data: <td>
- Extending XHTML
- Summary
- Q&A
- Workshop
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Introducing SVG
- Creating an SVG Document
- Creating Rectangles
- Adobe's SVG Viewer
- Using CSS Styles
- Creating Circles
- Creating Ellipses
- Creating Lines
- Creating Polylines
- Creating Polygons
- Creating Text
- Creating Gradients
- Creating Paths
- Creating Text Paths
- Creating Groups and Transformations
- Creating Animation
- Creating Links
- Creating Scripts
- Embedding SVG in HTML
- Introducing SMIL
- Summary
- Q&A
- Workshop
- Day 14. Handling XLinks, XPointers, and XForms
- Introducing XLinks
- Beyond Simple XLinks
- Introducing XPointers
- Introducing XBase
- Introducing XForms
- Summary
- Workshop
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Introducing the W3C DOM
- Introducing the DOM Objects
- Working with the XML DOM in JavaScript
- Searching for Elements by Name
- Reading Attribute Values
- Getting All XML Data from a Document
- Validating XML Documents by Using DTDs
- Summary
- Q&A
- Workshop
- Day 16. Using Java and .NET: DOM
- Using Java to Read XML Data
- Finding Elements by Name
- Creating an XML Browser by Using Java
- Navigating Through XML Documents
- Writing XML by Using Java
- Summary
- Q&A
- Workshop
- Day 17. Using Java and .NET: SAX
- An Overview of SAX
- Using SAX
- Using SAX to Find Elements by Name
- Creating an XML Browser by Using Java and SAX
- Navigating Through XML Documents by Using SAX
- Writing XML by Using Java and SAX
- Summary
- Q&A
- Workshop
- Day 18. Working with SOAP and RDF
- Introducing SOAP
- A SOAP Example in .NET
- A SOAP Example in Java
- Introducing RDF
- Summary
- Q&A
- Workshop
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Introducing DSOs
- Binding HTML Elements to HTML Data
- Binding HTML Elements to XML Data
- Binding HTML Tables to XML Data
- Accessing Individual Data Fields
- Binding HTML Elements to XML Data by Using the XML DSO
- Binding HTML Tables to XML Data by Using the XML DSO
- Searching XML Data by Using a DSO and JavaScript
- Handling Hierarchical XML Data
- Summary
- Q&A
- Workshop
- Day 20. Working with XML and Databases
- XML, Databases, and ASP
- Storing Databases as XML
- Using XPath with a Database
- Introducing XQuery
- Summary
- Q&A
- Workshop
- Day 21. Handling XML in .NET
- Creating and Editing an XML Document in .NET
- From XML to Databases and Back
- Reading and Writing XML in .NET Code
- Using XML Controls to Display Formatted XML
- Creating XML Web Services
- Summary
- Q&A
- Workshop
- Part V. In Review
- Appendix A. Quiz Answers
- Quiz Answers for Day 1
- Quiz Answers for Day 2
- Quiz Answers for Day 3
- Quiz Answers for Day 4
- Quiz Answers for Day 5
- Quiz Answers for Day 6
- Quiz Answers for Day 7
- Quiz Answers for Day 8
- Quiz Answers for Day 9
- Quiz Answers for Day 10
- Quiz Answers for Day 11
- Quiz Answers for Day 12
- Quiz Answers for Day 13
- Quiz Answers for Day 14
- Quiz Answers for Day 15
- Quiz Answers for Day 16
- Quiz Answers for Day 17
- Quiz Answers for Day 18
- Quiz Answers for Day 19
- Quiz Answers for Day 20
- Quiz Answers for Day 21
Introducing XQuery
Because XPath isn't really strong enough to let us handle databases, the W3C is creating XQuery. (In fact, the data model at the core of XQuery is going to be the basis of the next version of XPath, version 2.0.) Here's what the W3C says about XQuery:
- XML is a versatile markup language, capable of labeling the information content of diverse data sources including structured and semi-structured documents, relational databases, and object repositories. A query language that uses the structure of XML intelligently can express queries across all these kinds of data, whether physically stored in XML or viewed as XML via middleware. This specification describes a query language called XQuery, which is designed to be broadly applicable across many types of XML data sources.
The idea is for XQuery to be a query language something like SQL (which is used in database applications) that we can use with XML documents. XQuery is designed to let us access data much as if we were working with a database, even though we're working with XML. The creation of such a query language was inevitable—after all, XML's whole reason for existence is to provide a way to work with data. XQuery gives us not only a data model to let us interpret XML documents, but also a set of operators and functions to let us extract data from those documents.
Unfortunately, the W3C XQuery specification is very much up in the air at this time. It's been around a long time, but progress has been slow. This specification is divided into several working drafts; the main XQuery 1.0 working draft is at http://www.w3.org/TR/xquery, but there are also working drafts for XQuery semantics, the data model, and serialization. Here's a short list of what's available online as of this writing:
- The XQuery Activity Page (http://www.w3.org/XML/Query), which provides an overview of XQuery
- The XQuery version 1.0 Working Draft (http://www.w3.org/TR/xquery)
- The XQuery 1.0 and XPath 2.0 Data Model (http://www.w3.org/TR/xpath-datamodel)
- The XQuery 1.0 and XPath 2.0 Formal Semantics (http://www.w3.org/TR/xquery-semantics)
- The XML Syntax for XQuery 1.0 (XQueryX; http://www.w3.org/TR/xquery/xqueryx)
- The XQuery 1.0 and XPath 2.0 Functions and Operators (http://www.w3.org/TR/xquery/xpath-functions)
- The XML Query Requirements (http://www.w3.org/TR/xquery-requirements), which provides an overview of what's going to go into XQuery, in working draft form
Despite its slow progress, XQuery is very popular, and there are a number of implementations of XQuery 1.0 out there. Here's a starter list:
- The XQuery 1.0 Grammar Test Page (http://www.w3.org/2003/05/applets/xqueryApplet.html)
- The XPath 2.0 Grammar Test Page (http://www.w3.org/2003/05/applets/xpathApplet.html)
- BEA's Liquid Data (http://edocs.bea.com/liquiddata/docs10/prodover/concepts.html)
- Bluestream Database Software Corp.'s XStreamDB (http://www.bluestream.com/dr/?page=Home/Products/XStreamDB)
- Cerisent's XQE (http://cerisent.com/cerisent-xqe.html)
- Cognetic Systems's XQuantum (http://www.cogneticsystems.com/xquery/xquery.html)
- Enosys Software's XQuery Demo (http://xquerydemo.enosyssoftware.com)
- eXcelon's eXtensible Information Server (XIS 3.1 SP2) (http://www.exln.com/products/xis)
- Stylus Studio 4.5, with XQuery and XML Schema support (http://www.exln.com/products/stylusstudio)
- E-XMLMedia's XMLizer (http://www.e-xmlmedia.com/prod/xmlizer.htm)
- Fatdog's XQEngine (http://www.fatdog.com)
- GAEL's Derby (http://www.gael.fr/derby)
- GNU's Qexo (Kawa-Query; http://www.qexo.org), which compiles XQuery on-the-fly to Java bytecodes
- Ipedo's XML Database v3.0 (http://www.ipedo.com)
- IPSI's IPSI-XQ (http://ipsi.fhg.de/oasys/projects/ipsi-xq/index_e.html)
- Lucent's Galax (http://db.bell-labs.com/galax/), which is open source
- Microsoft's XML Query Language Demo (http://xqueryservices.com)
- Neocore's XML management system (XMS; http://www.neocore.com/products/products.htm)
- Nimble Technology's Nimble Integration Suite (http://www.nimble.com)
- OpenLink Software's Virtuoso Universal Server (http://demo.openlinksw.com:8890/xqdemo)
- Oracle's XML DB (http://otn.oracle.com/tech/xml/xmldb/htdocs/querying_xml.html)
- Politecnico di Milano's XQBE (http://dbgroup.elet.polimi.it/xquery/xqbedownload.html)
- QuiLogic's SQL/XML-IMDB (http://www.quilogic.cc/xml.htm)
- Software AG's Tamino XML Server (http://www.softwareag.com/tamino/News/tamino_41.htm)
- Tamino XML Query Demo (http://tamino.demozone.softwareag.com/demoXQuery/index.html)
- Sourceforge's Saxon (http://saxon.sourceforge.net/)
- SourceForge's XQuench (http://xquench.sourceforge.net/), which is open source
- SourceForge's XQuery Lite (http://sourceforge.net/projects/phpxmlclasses/)
- X-Hive's XQuery demo (http://www.x-hive.com/xquery)
- XML Global's GoXML DB (http://www.xmlglobal.com/prod/xmlworkbench)
Today, you're going to take a look at using XQuery with Lucent's Galax XQuery processor, one of the foremost XQuery implementations. You can download Galax for free at http://db.bell-labs.com/galax/, and you can see an online demo at http://db.bell-labs.com/galax/demo/galax_demo.html.
To use XQuery, we'll need an XML document, and we'll use the one shown in Listing 20.4, ch20_05.xml. This document contains data about some friends and a number of meetings arranged for their reunion, stored in <meeting> elements. We're going to use XQuery to extract information about the meetings and meeting locations.
Example 20.4. A Sample XML Document (ch20_05.xml)
<?xml version="1.0" encoding="UTF-8"?> <friends> <title>List of Friends</title> <friend>Ed Banachek</friend> <friend>Mark Up</friend> <friend>Wendy Thurston</friend> <friend>Becki Franks</friend> <meeting ID="introduction" time="evening" > <title>Introduction</title> <p>Getting to know everyone</p> <meeting> <title>Men</title> <p>Men's gathering</p> </meeting> <meeting> <title>Women</title> <p>Women's gathering</p> <location address="campus"> <title>Student Center</title> <phone number="555-1111"/> </location> <p>Just for fun</p> </meeting> </meeting> <meeting ID="breakfast" time="morning" > <title>Breakfast</title> <p>Breakfast meeting</p> <location address="cafeteria"> <title>Student Cafeteria</title> <phone number="555-1112"/> </location> <p>Just for fun</p> <meeting> <title>Men</title> <p>Men's gathering</p> </meeting> <meeting> <title>Women</title> <p>Women's gathering</p> <location address="campus"> <title>Student Dorm</title> <phone number="555-1113"/> </location> </meeting> <meeting> <title>Good Bye</title> <p>So long!</p> </meeting> </meeting> </friends>
To use Galax, you'll create two XQuery files. The first file will hold XQuery context code, where you'll declare the XML elements in ch20_05.xml and the XQuery functions you'll use. The other XQuery file will hold the template you'll use to query your XML document.
Start the XQuery context file, ch20_06.xq, by defining all the XML elements in your sample XML document so Galax can check the validity of that document. You can define the elements and attributes in your sample XML document easily, using this DTD-like syntax:
define element friends { element title, element friend+, element meeting+ } define element title {xsd:string} define element friend {xsd:string} define element meeting { attribute ID {xsd:string}?, attribute time {xsd:string}?, element title, (element p | element location | element meeting) * } define element p {xsd:string} define element location { attribute address { xsd:string }, element title, element phone } define element phone { attribute number {xsd:string} } define element meetingSummary { attribute ID {xsd:string}?, attribute time {xsd:string}?, element title, element locationCount {xsd:int}, element meetingSummary* }
This file sets the context for your XQuery work, so you'll connect your XML document ch20_05.xml to an XQuery variable, $friendlist, making the data in that document available to your XQuery code. XQuery variables are used to store data, and the name of an XQuery variable begins with a $ sign. In this case, you'll define a global variable (accessible in your XQuery template file) named $friendsList to hold the data from your XML document:
define global $friendsList {treat as document friends (glx:document-validate("ch20_05.xml", "friends"))}
We can also define functions in XQuery. We pass data to a function, and the code in the function can work on that data with XQuery statements and operators, and it returns the processed results. In this case you'll define a function named summary, which will return a summary of an element and display selected data from that element. You'll pass an element to this function, and start by getting the name of the element by using the built-in local-name function:
define function summary($elem as element) as element* { let $name := local-name($elem) . . . }
If you're dealing with a <meeting> element, you'll report this element in the results returned from this function, including its attributes, which you can refer to as $elem/@*. In the XML example, meetings themselves can have submeetings, so you'll loop over all the child elements of the current element and call summary recursively for them (items in curly braces, { and }, are evaluated by the XQuery engine and embedded in the output):
define function summary($elem as element) as element* { let $name := local-name($elem) return if ($name = "meeting") then <meeting> {$elem/@*} {for $item in $elem/* return summary($item)} </meeting> . . . }
This displays all the <meeting> elements and their <meeting> children. You'll also handle <title> elements in this function by returning the <title> element itself so that the title of any <meeting> elements will appear in the output:
define function summary($elem as element) as element* { let $name := local-name($elem) return if ($name = "meeting") then <meeting> {$elem/@*} {for $item in $elem/* return summary($item)} </meeting> else if ($name = "title") then $elem else () }
That's it for your XQuery context file, ch20_06.xq, which defines the syntax for your XML document, stores the data in that XML document as a global variable named $friendsList, and defines the summary function. Listing 20.5 shows the file ch20_06.xq.
Example 20.5. The XQuery Context Document (ch20_06.xq)
define element friends { element title, element friend+, element meeting+ } define element title {xsd:string} define element friend {xsd:string} define element meeting { attribute ID {xsd:string}?, attribute time {xsd:string}?, element title, (element p | element location | element meeting)* } define element p {xsd:string} define element location { attribute address { xsd:string }, element title, element phone } define element phone { attribute number {xsd:string} } define element meetingSummary { attribute ID {xsd:string}?, attribute time {xsd:string}?, element title, element locationCount {xsd:int}, element meetingSummary* } define global $friendsList {treat as document friends (glx:document-validate("ch20_05.xml", "friends"))} define function summary($elem as element) as element* { let $name := local-name($elem) return if ($name = "meeting") then <meeting> {$elem/@*} {for $item in $elem/* return summary($item)} </meeting> else if ($name = "title") then $elem else () }
Now you'll create an XQuery template file, ch20_07.xq, which will extract data from your XML document (stored in the $friendsList variable) and present those results as XML. For example, to get a summary of the <meeting> elements in your XML document, you'll create an element named <meetings> and display a summary of the <meeting> child elements of the <friends> document element in it. Note in particular the XPath-like syntax to specify the <meeting> child elements of the <friends> document element—$friendsList/friends/meeting:
<meetings> { for $meeting in $friendsList/friends/meeting return summary($meeting) } </meetings> ;
This code will strip out and display a summary of each <meeting> element—including all child <meeting> elements—while also preserving the <title> elements. Here's what the results look like:
<meetings> <meeting time="evening" ID="introduction"> <title>Introduction</title> <meeting><title>Men</title></meeting> <meeting><title>Women</title></meeting> </meeting> <meeting time="morning" ID="breakfast"> <title>Breakfast</title> <meeting><title>Men</title></meeting> <meeting><title>Women</title></meeting> <meeting><title>Good Bye</title></meeting> </meeting> </meetings>
You'll also display the locations of the various meetings by picking out the <location> elements in the XML document, preserving their attributes and titles, and displaying the results in a <locations> element, like this:
<locations> { for $location in $friendsList//location return <location> {$location/@*} {$location/title} </location> } </locations> ;
This code gives you the following results in the output, where you're displaying the <location> elements and their attributes, as well as any contained <title> elements:
<locations> <location address="campus"><title>Student Center</title></location> <location address="cafeteria"><title>Student Cafeteria</title></location> <location address="campus"><title>Student Dorm</title></location> </locations>
Use the XQuery count function to count the number of <meeting> elements. To count all <meeting> elements, no matter where they are in the input XML document, use the expression $friendsList//meeting (using the XPath-like // syntax to indicate any descendent), like this:
<meetingCount>{count($friendsList//meeting)}</meetingCount> ;
This gives you these results:
<meetingCount>7</meetingCount>
You can count the total number of <location> elements this way:
<locationCount>{count($friendsList//location)}</locationCount> ;
Here are the results:
<locationCount>3</locationCount>
You can also count the number of top-level meetings (remember that the <meeting> elements that themselves contain submeetings), like this:
<mainMeetingCount> { count($friendsList/friends/meeting) } </mainMeetingCount> ;
And this is what you'll see in the output:
<mainMeetingCount>2</mainMeetingCount>
Listing 20.6 shows the complete XQuery template file, ch20_07.xq.
Example 20.6. An XQuery Document (ch20_07.xq)
<meetings> { for $meeting in $friendsList/friends/meeting return summary($meeting) } </meetings> ; <locations> { for $location in $friendsList//location return <location> {$location/@*} {$location/title} </location> } </locations> ; <meetingCount>{count($friendsList//meeting)}</meetingCount> ; <locationCount>{count($friendsList//location)}</locationCount> ; <mainMeetingCount> { count($friendsList/friends/meeting) } </mainMeetingCount> ;
Now you'll pull it all together by using Galax on your context and template XQuery files. Here's how you do this:
%galax -context ch20_06.xq ch20_07.xq
And here are the results you get, showing how you've been able to extract and handle the data in ch20_05.xml and then format the results as XML:
%galax -context ch20_06.xq ch20_07.xq <meetings> <meeting time="evening" ID="introduction"> <title>Introduction</title> <meeting><title>Men</title></meeting> <meeting><title>Women</title></meeting> </meeting> <meeting time="morning" ID="breakfast"> <title>Breakfast</title> <meeting><title>Men</title></meeting> <meeting><title>Women</title></meeting> <meeting><title>Good Bye</title></meeting> </meeting> </meetings> <locations> <location address="campus"><title>Student Center</title></location> <location address="cafeteria"><title>Student Cafeteria</title></location> <location address="campus"><title>Student Dorm</title></location> </locations> <meetingList> <meeting title="Introduction" outsideLocations="0"/> <meeting title="Men" outsideLocations="0"/> <meeting title="Women" outsideLocations="1"/> <meeting title="Breakfast" outsideLocations="1"/> <meeting title="Men" outsideLocations="0"/> <meeting title="Women" outsideLocations="1"/> <meeting title="Good Bye" outsideLocations="0"/> </meetingList> <meetingCount>7</meetingCount> <locationCount>3</locationCount> <mainMeetingCount>2</mainMeetingCount>
You can also write these results to an XML document like this, which creates the document results.xml:
%galax -context ch20_06.xq ch20_07.xq -output-xml results. xml
In this section you've used Galax and XQuery to execute queries and extract data from your XML document. Not bad! Bear in mind, however, that XQuery is only at the working draft stage, and the kind of support you see here, and XQuery itself, will probably be changing rapidly.