By Priscilla Walmsley
Book Price: $49.99 USD
£35.50 GBP
PDF Price: $34.99
Cover | Table of Contents | Colophon
catalog.xml document is a product catalog containing general information about products (Example 1-1).
<catalog>
<product dept="WMN">
<number>557</number>
<name language="en">Fleece Pullover</name>
<colorChoices>navy black</colorChoices>
</product>
<product dept="ACC">
<number>563</number>
<name language="en">Floppy Sun Hat</name>
</product>
<product dept="ACC">
<number>443</number>
<name language="en">Deluxe Travel Bag</name>
</product>
<product dept="MEN">
<number>784</number>
<name language="en">Cotton Dress Shirt</name>
<colorChoices>white gray</colorChoices>
<desc>Our <i>favorite</i> shirt!</desc>
</product>
</catalog>
prices.xml document contains prices for the products, based on effective dates (Example 1-2).
<prices>
<priceList effDate="2006-11-15">
<prod num="557">
<price currency="USD">29.99</price>
<discount type="CLR">10.00</discount>
</prod>
<prod num="563">
<price currency="USD">69.99</price>
</prod>
<prod num="443">
<price currency="USD">39.99</price>
<discount type="CLR">3.99</discount>
</prod>
</priceList>
</prices>
doc("catalog.xml")/catalog/product
product elements from the catalog.xml document.doc("catalog.xml") calls an XQuery function named doc, passing it the name of the file to opencatalog selects the catalog element, the outermost element of the documentproduct selects all the product children of catalog
product elements, exactly as they appear (with the same attributes and contents) in the input document. Example 1-4 shows the complete result.
<product dept="WMN">
<number>557</number>
<name language="en">Fleece Pullover</name>
<colorChoices>navy black</colorChoices>
</product>
<product dept="ACC">
<number>563</number>
<name language="en">Floppy Sun Hat</name>
</product>
<product dept="ACC">
<number>443</number>
<name language="en">Deluxe Travel Bag</name>
</product>
<product dept="MEN">
<number>784</number>
<name language="en">Cotton Dress Shirt</name>
<colorChoices>white gray</colorChoices>
<desc>Our <i>favorite</i> shirt!</desc>
</product>
Query for $prod in doc("catalog.xml")/catalog/product where $prod/@dept = "ACC" order by $prod/name return $prod/name Results <name language="en">Deluxe Travel Bag</name> <name language="en">Floppy Sun Hat</name>
for
product nodes, and the rest of the FLWOR is evaluated once for each of the four products. Each time, a variable named $prod is bound to a different product element. Dollar signs are used to indicate variable names in XQuery.where
[@dept = "ACC"]) in a path expression.order by
return
product element's name children should be returned.let clause (the L in FLWOR) is used to set the value of a variable. Unlike a for clause, it does not set up an iteration. Example 1-6 shows a FLWOR that returns the same result as Example 1-5. The second line is a name elements in a ul element, for instance, you can use the query shown in Example 1-7. The ul element represents an unordered list in XHTML.Query <ul>{ for $product in doc("catalog.xml")/catalog/product where $product/@dept='ACC' order by $product/name return $product/name }</ul> Results <ul> <name language="en">Deluxe Travel Bag</name> <name language="en">Floppy Sun Hat</name> </ul>
ul start tag and end tag, and everything in between, is known as an element constructor. The curly braces around the content of the ul element signify that it is an expression (known as an enclosed expression) that is to be evaluated. In this case, the enclosed expression returns two elements, which become children of ul.
<h1>There are {count(doc("catalog.xml")//product)} products.</h1>
<h1>There are 4 products.</h1>
There are " and " products." appear literally in the results, as textual content of the ul element.doc function in a for clause, and the data function in an enclosed expression. Chapter 8 explains how to call functions and also describes how to write your own user-defined functions. Appendix A lists all of the built-in functions and explains each of them in detail.catalog.xml) and your order (order.xml). You want a list of all the items in the order, along with their number, name, and quantity.Query for $item in doc("order.xml")//item let $name := doc("catalog.xml")//product[number = $item/@num]/name return <item num="{$item/@num}" name="{$name}" quan="{$item/@quantity}"/> Results <item num="557" name="Fleece Pullover" quan="1"/> <item num="563" name="Floppy Sun Hat" quan="1"/> <item num="443" name="Deluxe Travel Bag" quan="2"/> <item num="784" name="Cotton Dress Shirt" quan="1"/> <item num="784" name="Cotton Dress Shirt" quan="1"/> <item num="557" name="Fleece Pullover" quan="1"/>
for clause sets up an iteration through each item from the order. For each item, the let clause goes to the product catalog and gets the name of the product. It does this by finding the product element whose number child equals the item's num attribute, and selecting its name child. Because the FLWOR iterated six times, the results contain one new item element for each of the six item elements in the order document. Joins are covered in Chapter 6.for clause to iterate over the list of distinct departments, a let clause to bind $items to the item elements for a particular department, and the sum function to calculate the totals of the quantity attribute values for the items in $items.Query for $d in distinct-values(doc("order.xml")//item/@dept) let $items := doc("order.xml")//item[@dept = $d] order by $d return <department name="{$d}" totQuantity="{sum($items/@quantity)}"/> Results <department name="ACC" totQuantity="3"/> <department name="MEN" totQuantity="2"/> <department name="WMN" totQuantity="2"/>
http://www.w3.org/XML/Query. The various recommendation documents are generally designed to be used by implementers of XQuery software, and they vary in readability and accessibility.http://www.w3.org/XML/Query. The various recommendation documents are generally designed to be used by implementers of XQuery software, and they vary in readability and accessibility.
doc or collection function in the query. Other queries operate on a set of input data that is set by the processor at the time the query is evaluated.
xs:integer, xs:string, and xs:date. The xs: prefix is used to indicate that these types are defined in the XML Schema specification. Types are assigned to items in the input document during schema validation, which is optional. If no schema is used, the items are untyped.
doc("order.xml")/order/substring(@num, 1, 4)
num attribute be declared to be of type xs:string. If it is untyped, it is cast to xs:string. In fact, if you do not plan to use a schema, you can in most cases use XQuery without any regard for types. However, if you do use a schema and the num attribute is declared to be of type xs:integer, you cannot use the preceding substring example without explicitly converting the value of the num attribute to xs:string, as in:
doc("order.xml")/order/substring(xs:string(@num), 1, 4)
xmlns. The prod prefix is mapped to the namespace http://datypic.com/prod. This means that any element or attribute name in the document that is prefixed with prod is in that namespace.<prod:product xmlns:prod="http://datypic.com/prod"> <prod:number>563</prod:number> <prod:name language="en">Floppy Sun Hat</prod:name> </prod:product>
Query declare namespace prod = "http://datypic.com/prod"; for $product in doc("prod_ns.xml")/prod:product return $product/prod:name Results <prod:name xmlns:prod="http://datypic.com/prod" language="en">Floppy Sun Hat</prod:name>
http://datypic.com/prod to the prefix prod2+3, to a complex composite expression like a FLWOR. Within a FLWOR, there may be other expressions, such as $prodDept = "ACC", which is a comparison expression, and doc("catalog.xml")/catalog/product, which is a path expression. Within these expressions, there are further expressions, such as "ACC", which is a literal, and $prodDept, which is a variable reference.|
Category
|
Description
|
Return to XQuery |
|---|