The document discusses XPath, which is a language for finding information in an XML document. It defines XPath syntax using path expressions to select nodes. It describes XPath terminology like nodes, relationships between nodes, and functions. Examples are provided to demonstrate XPath expressions for selecting elements, attributes, and filtering nodes. Predicates are also described for finding specific nodes or values.
4. Displaying XML with XSLT
• XSLT means eXtensible Stylesheet Language Transformations
• XSLT is the recommended style sheet language for XML.
• XSLT is far more sophisticated than CSS.
• With XSLT, one can
– Add/remove elements and attributes to or from the output file.
– Rearrange and sort elements,
– Perform tests and make decisions about which elements to hide and
display, and a lot more.
5. XSLT Example
• XSLT uses XPath to find information in an XML document.
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty
of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with
strawberries and whipped cream</description>
<calories>900</calories>
</food>
7. XSLT Example
• XSLT uses XPath to find information in an XML document.
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;
background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="name"/> -
</span>
<xsl:value-of select="price"/>
</div>
8. XSLT Example
• XSLT uses XPath to find information in an XML document.
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="description"/>
<span style="font-style:italic">
(<xsl:value-of select="calories"/> calories per serving)
</span>
</p>
</div>
</xsl:for-each>
</body>
</html>
12. Introduction
• What is XPath?
– XPath is a syntax for defining parts of an XML document
– XPath uses path expressions to navigate in XML documents
– XPath contains a library of standard functions
– XPath is a major element in XSLT
– XPath is also used in XQuery, XPointer and XLink
– XPath is a W3C recommendation
• XPath was created
– To reduce the amount of time to find the elements and attributes
desired by an author
13. XPath Path Expressions
• XPath uses path expressions
– To select nodes or node-sets in an XML document.
• XPath expressions can be used in
– JavaScript,
– Java,
– XML Schema,
– PHP,
– Python,
– C and C++, and lots of other languages.
14. XPath Standard Functions
• XPath includes over 100 built-in functions.
• There are functions for
– String values,
– Numeric values,
– Date and time comparison,
– Node and QName manipulation,
– Sequence manipulation,
– Boolean values, and more.
15. XPath is Used in XSLT
• XPath was designed to be used by
– XSLT,
– XPointer and
– Other XML parsing software.
• Without XPath knowledge
– You will not be able to create XSLT documents.
16. XPath Terminology - Nodes
• XML documents are treated as trees of nodes.
• Topmost element of the tree is called the root element.
• In XPath, there are seven kinds of nodes:
– Element, attribute, text, namespace, processing-instruction, comment,
and document nodes.
18. Relationship of Nodes
• Parent - Each element and attribute has one parent.
• Children - Element nodes may have zero, one or more children.
• Siblings - Nodes that have the same parent.
• Ancestors - A node's parent, parent's parent, etc.
• Descendants - A node's children, children's children, etc.
• In this example;
– book element is the parent of the title, author, year, and price
– title, author, year, and price elements are all children of book element
– title, author, year, and price elements are all siblings
– ancestors of title element are book element and bookstore element:
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
Descendants of bookstore element
are the book, title, author, year,
and price elements:
22. Predicates
Path Expression Result
/bookstore/book[last()-1]
Selects the last but one book element that is the child of
the bookstore element
/bookstore/book[position()<3]
Selects the first two book elements that are children of
the bookstore element
//title[@lang]
Selects all the title elements that have an attribute
named lang
//title[@lang='en']
Selects all the title elements that have a "lang" attribute
with a value of "en"
/bookstore/book[price>35.00]
Selects all the book elements of the bookstore element
that have a price element with a value greater than
35.00
/bookstore/book[price>35.00]/title
Selects all the title elements of the book elements of the
bookstore element that have a price element with a
value greater than 35.00
23. Selecting Unknown Nodes
• XPath wildcards can be used to select unknown XML nodes.
• Example:
Wildcard Description
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind
Path Expression Result
/bookstore/*
Selects all the child element nodes of the
bookstore element
//* Selects all elements in the document
//title[@*]
Selects all title elements which have at least
one attribute of any kind
24. Selecting Several Paths
• By using the | operator in an XPath expression you can select several paths.
• Some path expressions and the result of the expressions:
Path Expression Result
//book/title | //book/price
Selects all the title AND price elements of all book
elements
//title | //price
Selects all the title AND price elements in the
document
/bookstore/book/title | //price
Selects all the title elements of the book element of
the bookstore element AND all the price elements in
the document
27. XPath Syntax
• XPath uses path expressions
– To select nodes in an XML document
• Node is selected by following a path or steps.
Expression Description
nodename Selects all nodes with the name "nodename"
/ Selects from the root node
//
Selects nodes in the document from the current node that
match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes
28. XPath Syntax
• XPath uses path expressions
– To select nodes in an XML document
• Node is selected by following a path or steps.
Expression Description
nodename Selects all nodes with the name "nodename"
/ Selects from the root node
//
Selects nodes in the document from the current node that
match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes