- Overview
- Table of Contents
- The Document Object Model
- DOM and Java
- DOM and JavaScript
- DOM and .NET
- DOM and C++
- DOM and Perl
- DOM and PHP
- DOM Level 3
- The Simple API for XML (SAX)
- SAX and Java
- SAX and .NET
- SAX and Perl
- SAX and PHP
- Validation
- Document Type Definitions (DTDs)
- XML Schemas
- RELAX NG
- Schematron
- Validation in Applications
- XSL Transformations (XSLT)
- XSLT in Java
- XSLT and RSS in .NET
- XSL-FO
- XPath
- XML Base
- XHTML
- XHTML 2.0
- Cascading Style Sheets
- XUL
- XML Events
- XML Data Binding
- XML and Databases
- SQL Server and FOR XML
- Service Oriented Architecture
- Web Services
- Apache Axis2
- REST
- SOAP
- SOAP and Java
- WSDL
- UDDI
- XML-RPC
- Ajax
- JSON
- Ruby on Rails
- Web Services Security
- SAML
- XML Digital Signatures
- XML Key Management Services
- Internationalization
- Grid Computing
- Web Services Resource Framework
- WS-Addressing
- WS-Notifications
- New Languages: XML in Use
- Google Web Toolkit
- Google Sitemaps
- Accessibility
- The Semantic Web
- WML
- Google Web Services
- The Yahoo! Web Services Interface
- eBay REST API
- WordML
- DocBook
- XML Query
- XForms
- Resource Description Framework (RDF)
- Topic Maps
- Rich Site Summary (RSS)
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Scalable Vector Graphics (SVG)
- OPML
- Summary
- Projects
- Additional Resources
XML Query
Last updated Jan 1, 2004.
It doesn't take long to realize that there's a big difference between selecting data from a database using SQL and selecting it from an XML file using XPath. For one thing, an XPath statement is just that: one statement. An SQL statement is typically one statement as well, but most databases have ways to combine statements, such as PL/SQL or Transact SQL.
XML Query, combined with XPath, enables you to create a lot of the same functionality in an XML-esque statement. Consider, for a moment, the hardware order file:
<?xml version="1.0"?>
<order orderid="THX1138" customerNumber="3263827">
<lineitem itemid="C33">
<item>3/4" Hex Bolt</item>
<quantity>36</quantity>
<unitprice currency="dollars">.35</unitprice>
</lineitem>
<lineitem itemid="M48">
<item>Condenser</item>
<quantity>1</quantity>
<unitprice currency="dollars">2200</unitprice>
</lineitem>
<delivery>Overnight</delivery>
</order>
We can create a simple file that lists the items that were ordered using XML Query:
<orderInformation>
{
for $item in //item
return
<itemInfo>
{ $item }
</itemInfo>
}
</orderInformation>
This is a lot like the for-each (or for-next) statement you find in a
lot of langauges. For each node that's returned by the XPath statement,
//item, that node gets assigned to the $item variable, and gets output
as part of the return statement. In this case, you'd wind up with output
of:
<orderInformation>
<itemInfo>
<item>3/4" Hex Bold</item>
</itemInfo>
<itemInfo>
<item>Condenser</item>
</itemInfo>
</orderInformation>
XML Query also enables you to do a straight assignment of a series of nodes to a variable, using the LET statment:
<orderInformation>
{
let $item := //item
return
<itemInfo>
{ $item }
</itemInfo>
}
</orderInformation>
This would give you:
<orderInformation>
<itemInfo>
<item>3/4" Hex Bold</item>
<item>Condenser</item>
</itemInfo>
</orderInformation>
You can also add a qualifier to an XML Query statement, in the form of a WHERE clause:
<orderInformation>
{
let $item := //item
where $item[@itemid="M48"]
return
<itemInfo>
{ $item }
</itemInfo>
}
</orderInformation>
This restriction makes the output:
<orderInformation>
<itemInfo>
<item>Condenser</item>
</itemInfo>
</orderInformation>
You can also go ahead and combine these clauses to form a FOR-LET-WHERE-RETURN statement, also known as a FLWR statement:
<orderInformation>
{
for $line in //lineitem
let $item := $line/item, $qty := $line/quantity
where $item[@itemid="M48"]
return
<itemInfo>
{ $quantity }
{ $item }
</itemInfo>
}
</orderInformation>
Notice that in addition to combining the FOR and LET clauses, we're
defining more than one variable in the LET clause. Notice also that the
LET clause uses the $line variable directly. We wind up with:
<orderInformation>
<itemInfo>
<quantity>1</quantity>
<item>Condenser</item>
</itemInfo>
</orderInformation>
This is, of course, just a limited idea of what you can do with XML Query. Check out the resources for more ideas.






Account Sign In
View your cart