程序代写代做代考 database file system XML Documents

XML Documents

XML Documents

Miao Qiao

SEAT
Massey University

1

Querying XML Documents

• XPath
• XSLT
• XQuery

2

XPath

The primary purpose of XPath is to address parts of an XML document.

• Tree representation of an XML document.
• XPath overview.
• XPath expression.

3

XPath: Tree Representation of an XML Document

4

XPath: Tree Representation of an XML Document
XPath models a XML document as a tree of 7 types of nodes. Below lists
4 major types:
• root nodes
• element nodes
• text nodes
• attribute nodes

Parent, child, sibling, and descendant:
• Parent: every node other than the root node has exactly one parent,

which is either an element node or the root node.
• Child: root nodes and element nodes have an ordered list of child

nodes.
• Sibling: another child of the parent of the current node.
• Descendant: the descendants of a node are the children of the node

and the descendants of the children of the node.

5

XPath: Tree Representation of an XML Document
XPath models a XML document as a tree of 7 types of nodes. Below lists
4 major types:
• root nodes
• element nodes
• text nodes
• attribute nodes

Parent, child, sibling, and descendant:
• Parent: every node other than the root node has exactly one parent,
which is either an element node or the root node.
• Child: root nodes and element nodes have an ordered list of child
nodes.
• Sibling: another child of the parent of the current node.
• Descendant: the descendants of a node are the children of the node
and the descendants of the children of the node.

5

XPath: Overview

XML document
XPath expression

}
=⇒ XPath Evaluation =⇒




node-set,
boolean,
number,
or string

6

XPath: Overview

XML document
XPath expression

}
=⇒ XPath Evaluation

=⇒




node-set,
boolean,
number,
or string

6

XPath: Overview

XML document
XPath expression

}
=⇒ XPath Evaluation =⇒




node-set,
boolean,
number,
or string

6

XPath: Overview

An XPath expression has five constructs:
• Location path.
• Function call.
• Boolean, Number, or String.

Example: See document “Bookstore-DTD.xml”

• Location path: /Bookstore//@ISBN
• Function call: count(/Bookstore//@ISBN)
• Number: 3+count(/Bookstore//@ISBN)

7

XPath: Evaluation with XMLlint

Command line:
xmllint –xpath “xpath expression” xmlDocumentName”

Example:
xmllint –xpath “3+count(/Bookstore//@ISBN)” Bookstore-DTD.xml

8

XPath: Overview

An XPath expression returns 4 types of objects:
• node-set (an unordered collection of nodes without duplicates)
• boolean, number, or string

Example: See document “Bookstore-DTD.xml”
• Node set: /Bookstore/*
• Number: count(/Bookstore/*)
• String: /Bookstore//Title/text()
• Boolean: count(/Bookstore/*)=3, count(/Bookstore/*)=2

9

XPath: Local Paths
An XPath expression has five constructs:
• Location path: The most important construct of XPath.
• Function call.
• Boolean, Number, or String.

Two kinds of location paths (similar to the file system location):
• Relative location path: a relative location path consists of a sequence

of one or more location steps separated by /.
• Absolute location path: /a relative location path

Example:
• Relative location path: Book/Title

Two steps involved: Book and Title.
• Absolute location path: /Bookstore/Book

10

XPath: Local Paths
An XPath expression has five constructs:
• Location path: The most important construct of XPath.
• Function call.
• Boolean, Number, or String.

Two kinds of location paths (similar to the file system location):
• Relative location path: a relative location path consists of a sequence
of one or more location steps separated by /.
• Absolute location path: /a relative location path

Example:
• Relative location path: Book/Title

Two steps involved: Book and Title.
• Absolute location path: /Bookstore/Book

10

XPath: Local Paths
An XPath expression has five constructs:
• Location path: The most important construct of XPath.
• Function call.
• Boolean, Number, or String.

Two kinds of location paths (similar to the file system location):
• Relative location path: a relative location path consists of a sequence
of one or more location steps separated by /.
• Absolute location path: /a relative location path

Example:
• Relative location path: Book/Title
Two steps involved: Book and Title.
• Absolute location path: /Bookstore/Book

10

XPath: Local Paths

Each location step has a context node, which is the “base node” of the
step.

A location step has two main parts:
• an axis: pecifies the tree relationship between the nodes selected by
the location step and the context node;
• zero or more predicates: use arbitrary expressions to further refine the
set of nodes selected by the location step.

11

XPath: Local Paths (Axis)
The axis specifies the relation between node-to-be-selected and the
context node, which could be:
• ’self’
• ’ancestor’
• ’ancestor-or-self’
• ’attribute’
• ’child’
• ’descendant’
• ’descendant-or-self’
• ’following’
• ’following-sibling’
• ’namespace’
• ’parent’
• ’preceding’
• ’preceding-sibling’

12

XPath: Local Paths (Axis)

Example of axis: See document “Bookstore-DTD.xml”
• ’self’ /Bookstore/Book/self::*
• ’ancestor’ /Bookstore/Book/ancestor::*
• ’attribute’ /Bookstore/Book/attribute::*
• ’child’ /Bookstore/Book/child::*
• ’descendant’ /Bookstore/Book/descendant::*
• ’following’ /Bookstore/Book/following::*
• ’following-sibling’
/Bookstore/Book/Authors/Author[1]/following-sibling::*
• ’parent’ /Bookstore/Book/Authors/Author[3]/parent::*

13

XPath: Local Paths (Predicates)

The constructs of a predicate expression: — The most important part
of Local Paths.
Predicate:
• Merriam Webster: something that is affirmed or denied of the subject
in a proposition in logic
• In our context: A square bracket embraced predicate expression.

Example: Book[@Price=75].

14

XPath: Local Paths (Predicates)

A predicate expression expr is defined as one of the following:
• variable reference: refer to a node of an XML document.
• exprA or exprB: returns true if either exprA or exprB is true.
• exprA and exprB: returns true if both exprA and exprB is true.
• exprA=exprB: returns true if left equals to right; ! = means not equal.
• <=, >=, <, >
• string.
• integer.
• functional call.

This is a recursive definition where exprA and exprB are both predicate
expressions.

15

XPath: Local Paths (Predicates)

Example: see document “Bookstore-DTD.xml”.
• Existence: Book[@Price], Book[@Garden], Book[Remark]/@ISBN
• Numeric comparison: Book[@Price=85], Book[@Price>90]
• And: Book[@Price>10 and @Edition]/@ISBN
• Child selection: Book[2]
• Expression concatenation: Book[@Price>90][2], Book[@Price>10][@Edition]
• Expression indention: Book[@Price>90 and

Authors[Author/First_Name=’Jeffrey’]]/@ISBN

16

XPath: Local Paths (Predicates)

Example: see document “Bookstore-DTD.xml”.
• //Book[Authors/Author/First_Name=’Hector’ and

Authors/Author/Last_Name=’Garcia-Molina’]/@ISBN”
• //Book[//First_Name=’Hector’ and //Last_Name=’Garcia-Molina’]/@ISBN”
• //Book[Authors/Author/First_Name=’Hector’ and

Authors/Author/Last_Name=’Widom’]/@ISBN”
• //Book[contains(Title, ’Complete’)]/@ISBN
• //Book[contains(.//Author, ’Jeffrey’) and contains(.//Author, ’Jenifer’)]
• //Book[contains(.//Author, ’Jeffrey’) and contains(.//Author, ’Ullman’)]/@ISBN
• //Book[@price>10]/@ISBN

17

XPath: Local Paths

An XPath expression has five types:
• Location path: The most important construct of XPath.
• Function call.
• Boolean, Number, or String.

18

XPath: Function call

There are 200+ functions supported by XPath. Learn as you go in future.
• count(a node set)
• contains(string, string)

19

Abbreviated Syntax

• . : selects the context node
• .// : selects the context node and all its descendants
• .. : selects the parent of the context node
• *: selects all children element of the context node
• Author: selects Author elements among the children of the context.
• @ISBN: selects the ISBN attribute of the context node
• text(): selects all text node children of the context node
• @*: selects all the attributes of the context node
• Author[1]: selects the first Author child of the context node
• Author[last()]: selects the last Author child of the context node
• */Author : selects all Author grandchildren of the context node
• //Author : selects all the Author descendants of the document root
• //Author/First_Name
• ../@ISBN

20

XPath: References

• W3Cschool: https://www.w3schools.com/
• W3Crecommendation: https://www.w3.org/TR/xpath/
• Chapter 10, “Database System Concepts (Fifth Edition)”, A.
Silberschatz, H. F. Korth, S. Sudarshan.
• Introduction to Databases, Stanford University.

21

https://www.w3schools.com/
https://www.w3.org/TR/xpath/

XML Validation
XML Validation: DTD
XML Validation: XSD

Query
XPath