Download XML Querying with XQuery: Selecting, Filtering, and Transforming XML Data and more Slides Semantics of Programming Languages in PDF only on Docsity!
XML Query: xQuery
XML Data
- Enormous amount of information is now stored in XML,
- This includes highly structured data, such as sales figures,
- Semi-structured data such as product catalogs and yellow pages,
- and relatively unstructured data such as letters and books.
- Even more information is passed between systems as transitory XML documents.
- All of this data is used for a variety of purposes
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
557 Fleece Pullover navy black 563 Floppy Sun Hat 443 Deluxe Travel Bag 784 Cotton Dress Shirt white gray Our favorite shirt!
catalog.xml
doc("catalog.xml")/catalog/product
- doc("catalog.xml") calls an XQuery function named
doc, passing it the name of the file to open
- catalog selects the catalog element, the outermost
element of the document
- product selects all the product children of catalog
- The result of the query will be the four product
elements, exactly as they appear (with the same attributes and contents) in the input document
557 Fleece Pullover navy black 563 Floppy Sun Hat 443 Deluxe Travel Bag 784 Cotton Dress Shirt white gray Our favorite shirt!
Path Expressions
- Path expressions are convenient because of
their compact, easy-to-remember syntax.
- However, they have a limitations:
- 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.
xQuery - FLWOR
- The basic structure of many (but not all)
queries is the FLWOR expression
FLWOR
- 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 Docsity.com
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
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
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
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.
Wrap the results
- wrap the results of your query in a different
XML vocabulary **Query
{** for $product in doc("catalog.xml")/catalog/product where $product/@dept='ACC' order by $product/name return $product/name **}
……. Results**
Deluxe Travel Bag Floppy Sun Hat