1 / 23

XML Query: xQuery

XML Query: xQuery. Reference: Xquery By Priscilla Walmsley , Published by O’Reilly. XQuery. Enormous amount of information is now stored in XML, both in XML databases and in documents on a filesystem All of this data is used for a variety of purposes

zhen
Télécharger la présentation

XML Query: xQuery

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Query: xQuery Reference: Xquery By Priscilla Walmsley, Published by O’Reilly

  2. XQuery • Enormous amount of information is now stored in XML, both in XML databases and in documents on a filesystem • All of this data is used for a variety of purposes • XQuery is a query language designed by the W3C to address these needs • It allows you to select the XML data elements of interest, reorganize and possibly transform them, and return the results in a structure as you want

  3. Capabilities of XQuery • Selecting information based on specific criteria • Filtering out unwanted information • Searching for information within a document or set of documents • Joining data from multiple documents or collections of documents • Sorting, grouping, and aggregating data • Transforming and restructuring XML data into another XML vocabulary or structure • Performing arithmetic calculations on numbers and dates • Manipulating strings to reformat text

  4. Path Expressions • Simply selects elements or attributes from an input document: Examples • doc("catalog.xml")/catalog/product • doc("catalog.xml")/*/product/@dept • doc("catalog.xml")//product/@dept • doc("catalog.xml")/catalog/product[@dept = "ACC"] • doc("catalog.xml")/catalog/product[2]

  5. Path Expressions • Path expressions are convenient because of their compact, easy-to-remember syntax. • However, they have a limitation: • they can only return elements and attributes as they appear in input documents. • Any elements selected in a path expression appear in the results with the same names, the same attributes and contents, • and in the same order as in the input document.

  6. FLWORs • stands for “for, let, where, order by, return” • FLWORs, unlike path expressions, allow you to manipulate, transform, and sort your results. • A Simple Query for $prod in doc("catalog.xml")/catalog/product where $prod/@dept = "ACC" order by $prod/name return $prod/name

  7. FLWORs • For • This clause sets up an iteration through the 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. for $prod in doc("catalog.xml")/catalog/product where $prod/@dept = "ACC" order by $prod/name return $prod/name

  8. FLWORs • where • This clause selects only products in the ACC department. • This has the same effect as a predicate ([@dept = "ACC"]) in a path expression. for $prod in doc("catalog.xml")/catalog/product where $prod/@dept = "ACC" order by $prod/name return $prod/name

  9. FLWORs • order by • This clause sorts the results by product name, something that is not possible with path expressions. for $prod in doc("catalog.xml")/catalog/product where $prod/@dept = "ACC" order by $prod/name return $prod/name

  10. FLWORs • order by • This clause sorts the results by product name, something that is not possible with path expressions. for $prod in doc("catalog.xml")/catalog/product where $prod/@dept = "ACC" order by $prod/name return $prod/name

  11. FLWORs • return • This clause indicates that the product element’s name children should be returned. for $prod in doc("catalog.xml")/catalog/product where $prod/@dept = "ACC" order by $prod/name return $prod/name

  12. FLWORs • The let clause (the L in FLWOR) is used to set the value of a variable • second line is a let clause that assigns the product element’s name child to a variable called $name. • The $name variable is then referenced later in the FLWOR, in both the order by clause and the return clause for $product in doc("catalog.xml")/catalog/product let $name := $product/name where $product/@dept = "ACC" order by $name return $name

  13. FLWORs • The let clause serves as a programmatic convenience that avoids repeating the same expression multiple times. • Using some implementations, it can also improve performance, because the expression is evaluated only once instead of each time it is needed.

  14. Wrap the results • wrap the results of your query in a different XML vocabulary 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>

  15. Another Example Query: <h1>There are {count(doc("catalog.xml")//product)} products.</h1> Results: <h1>There are 4 products.</h1>

  16. You can include element constructors at various places in your query Query: <ul>{ for $product in doc("catalog.xml")/catalog/product where $product/@dept='ACC' order by $product/name return <li>{$product/name}</li> }</ul> Results: <ul> <li><name language="en">Deluxe Travel Bag</name></li> <li><name language="en">Floppy Sun Hat</name></li> </ul>

  17. attributes to results and data function • You can also add your own attributes to results using an XML-like syntax Query <ultype="square">{ for $product in doc("catalog.xml")/catalog/product where $product/@dept='ACC' order by $product/name return <li class="{$product/@dept}">{data($product/name)}</li> }</ul> Results <ul type="square"> <li class="ACC">Deluxe Travel Bag</li> <li class="ACC">Floppy Sun Hat</li> </ul>

  18. Functions • There are over 100 functions built into XQuery, covering a broad range of functionality. • Functions can be used to manipulate strings and dates, perform mathematical calculations, combine sequences of elements, and perform many other useful jobs. • You can also define your own functions, either in the query itself, or in an external library • Both built-in and user-defined functions can be called from almost any place in a query.

  19. Typical function call • substring($prodName, 1, 5) • where the name of the function is substring and there are three arguments, separated by commas and surrounded by parentheses. The first argument is a variable reference, whereas the other two are numeric literals. • XQuery comments, delimited by (: and :) • (: This query returns the <number> children :)

  20. Evaluation Order and Parentheses • if ($x < 12 and $y > 0) then $x + $y else $x - $y • if (($x < 12) and ($y > 0)) then ($x + $y) else ($x - $y) • any and operators are evaluated before or operators • true() and true() or false() and false( ) • (true() and true( )) or (false() and false( ))

  21. Conditional (if-then-else) Expressions Query for $prod in (doc("catalog.xml")/catalog/product) return if ($prod/@dept = 'ACC') then <accessoryNum>{data($prod/number)}</accessoryNum> else <otherNum>{data($prod/number)}</otherNum> Results <otherNum>557</otherNum> <accessoryNum>563</accessoryNum> <accessoryNum>443</accessoryNum> <otherNum>784</otherNum>

  22. Conditional expression returning multiple expressions Query for $prod in (doc("catalog.xml")/catalog/product) return if ($prod/@dept = 'ACC') then (<accessoryNum>{data($prod/number)}</accessoryNum>, <accessoryName>{data($prod/name)}</accessoryName>) else <otherNum>{data($prod/number)}</otherNum> Results <otherNum>557</otherNum> <accessoryNum>563</accessoryNum> <accessoryName>Floppy Sun Hat</accessoryName> <accessoryNum>443</accessoryNum> <accessoryName>Deluxe Travel Bag</accessoryName> <otherNum>784</otherNum>

  23. XQuery and XML Schema • XQueryuses the type system of XML Schema, which includes built-in types that represent common datatypessuch as decimal, date, and string. • XML Schema also specifies a language for defining your own types based on the built-in types. • For example, if your item element has a quantity attribute, and you know from the schema that the type of the quantity attribute is xs:integer, you can perform sorts or other operations on that attribute’s value without converting it to an integer in the query

More Related