Home > Guides

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.

Discussions

JavaScript and XSLT
Posted Jun 12, 2008 03:40 PM by cjalkam
0 Replies
delete node
Posted May 17, 2008 07:42 AM by fiatydave
0 Replies
passing java instances into xslt
Posted Apr 4, 2008 02:17 PM by tdr50040830
0 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer BortelFebruary 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

So Far So Good
By John TraenkenschuhFebruary 2, 2010 No Comments

So far, Win 7 is making a thoroughbred of what has been a plough mule laptop

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin SullivanFebruary 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

See More Blogs

Informit Network