Docsity

XML Query Languages: XPath, XQuery, and XSLT, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download XML Query Languages: XPath, XQuery, and XSLT and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Query Languages for XML

XPath XQuery XSLT

The XPath/XQuery Data Model

  • Corresponding to the fundamental “relation” of the relational model is: sequence of items.
  • An item is either:
    1. A primitive value, e.g., integer or string.
    2. A node (defined next).

Document Nodes

  • Formed by doc(URL) or document(URL).
  • Example: doc(/usr/class/cs145/bars.xml)
  • All XPath (and XQuery) queries refer to a doc node, either explicitly or implicitly. - Example: key definitions in XML Schema have Xpath expressions that refer to the document described by the schema.

DTD for Running Example

]>

Nodes as Semistructured Data

7

BARS

PRICE PRICE

BAR (^) ”JoesBar”name = BEER

theBeer = ”Miller”

theBeer = ”Bud”

SoldBy = ”…”

name = ”Bud”

2.50 3.

Rose =document Green = element Gold = attribute Purple = primitive value

bars.xml

Paths in XML Documents

  • XPath is a language for describing paths in XML documents.
  • The result of the described path is a sequence of items.

Evaluating a Path Expression

  • Assume the first tag is the root.
    • Processing the doc node by this tag results in a sequence consisting of only the root element.
  • Suppose we have a sequence of items, and the next tag is X. - For each item that is an element node, replace the element by the subelements with tag X.

Example: /BARS

2.50 3.00 … <BEER name = ”Bud” soldBy = ”JoesBar SuesBar … ”/> …

11

One item, the BARS element Docsity.com

Example: /BARS/BAR/PRICE

2.50 3.00 … <BEER name = ”Bud” soldBy = ”JoesBar SuesBar …”/> …

13

These PRICE elements followed by the PRICE elements of all the other bars.

Attributes in Paths

  • Instead of going to subelements with a given tag, you can go to an attribute of the elements you already have.
  • An attribute is indicated by putting @ in front of its name.

Remember: Item Sequences

  • Until now, all item sequences have been sequences of elements.
  • When a path expression ends in an attribute, the result is typically a sequence of values of primitive type, such as strings in the previous example.

Paths that Begin Anywhere

  • If the path starts from the document node and begins with // X , then the first step can begin at the root or any subelement of the root, as long as the tag is X.

Wild-Card *

  • A star (*) in place of a tag represents any one tag.
  • Example: ///PRICE represents all price objects at the third level of nesting.

Example: /BARS/*

2.50 3.00 … <BEER name = ”Bud” soldBy = ”JoesBar SuesBar … ”/> …

(^20)

This BAR element, all other BAR elements, the BEER element, all other BEER elements