Object Oriented Programming III1 XSLT Processing XML using XSLT Using XPath.
Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose...
-
Upload
virgil-harrison -
Category
Documents
-
view
215 -
download
0
Transcript of Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose...
![Page 1: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/1.jpg)
Navigating XML
![Page 2: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/2.jpg)
Overview
Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an XML document. In support of
this primary purpose, it also provides basic facilities for manipulation of strings, numbers and booleans. XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values. XPath operates on the abstract, logical structure of an XML document, rather than its surface syntax. XPath gets its name from its use of a path notation as in URLs for navigating through the hierarchical structure of an XML document.
![Page 3: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/3.jpg)
The Xpath Expression
An Xpath expression consists of a location path and one or more location steps.
Each location step consists of an axis, a node test and a predicateaxis::nodetest[predicate]
One or more of these elements may be absent but if both axis and nodetest are present they must be separated by :: and if the predicate is present it must be in [ ]
![Page 4: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/4.jpg)
First a Few Terms
Node Node-set Context Node Location Path
![Page 5: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/5.jpg)
Node
There are seven types of nodes The document as a whole Element nodes (one for each element in
a document) Attribute nodes Comment nodes Processing instructions Namespace nodes Text nodes
![Page 6: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/6.jpg)
Node-set
A node-set is a group of nodes returned by a single xpath expression
(This has important implications for Xpointers where sets of information could be returned from a remote document and embedded in a new or current document)
![Page 7: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/7.jpg)
Context-node
The context node identifies where in the document to start applying a given set of instructions
![Page 8: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/8.jpg)
Location Path
The location path is simply the path or instructions for getting to a particular point in a document
![Page 9: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/9.jpg)
Axis
The axis determines which direction you want to move in the document
Directions are like “next node”, preceding node”, “Parent node”, “Child node”
![Page 10: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/10.jpg)
Some Axis Directions
childchild One step down the element treeOne step down the element tree
parentparent One step up the element treeOne step up the element tree
attributeattribute Looks at the attributes of the context nodeLooks at the attributes of the context node
ancestorancestor Looks up the tree but doesn’t include Looks up the tree but doesn’t include context node itselfcontext node itself
descendentdescendent Reverse of ancestorReverse of ancestor
followingfollowing All the nodes after the context nodesAll the nodes after the context nodes
precedingpreceding All the nodes before the context nodesAll the nodes before the context nodes
namespacenamespace Gets all the namespace nodes for the Gets all the namespace nodes for the current elementcurrent element
selfself Just the context node itselfJust the context node itself
![Page 11: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/11.jpg)
Axis Short Cut Syntax
child::orderchild::order orderorder Child is the defaultChild is the default
order/*order/*
order/@order/@
Selects any child of orderSelects any child of order
Selects any attribute of Selects any attribute of orderorder
order::attribute::order::attribute::
quantityquantity
order/order/@quantity@quantity
Selects attribute quantity Selects attribute quantity from order elementfrom order element
descendant-or-descendant-or-self::orderself::order
//order//order Any descendent of order or Any descendent of order or the order element itselfthe order element itself
![Page 12: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/12.jpg)
Nodetest
There are three kinds of node test Name test—tests the names along the
specified path Node types—tests for the type of node along
the specified path (comment, node, processing-instruction or text)
Literal text—locates the literal text
preceding::processing-instruction(“xml:stylesheet”)
![Page 13: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/13.jpg)
Predicate
The predicate part is optional, but can give you more precise control
//customer[@name='John']/@name This has a predicate of
[@name=‘John’] which selects the order that has the name attribute value John
![Page 14: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/14.jpg)
Xpath Function
Xpath also contains many built in functions. Some are:
position()position() ceiling ()ceiling ()
last()last() floor ()floor ()
sum ()sum () round ()round ()
count ()count () substring ()substring ()
![Page 15: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/15.jpg)
An Example: The XML File
<?xml version=“1.0”?><orders> <customer name=“John” > <order quantity=“5”> dinner specials </order> <order quantity=“2”> red wine </order> </customer></orders>
*click here to see the whole file
![Page 16: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/16.jpg)
Selecting one Customer
<h3><xsl:value-of select="//customer[@name='John']/@name"/></h3>
<p><xsl:for-each select="//customer[@name='John']/order">
<xsl:value-of select="@quantity" /><xsl:value-of select="." /></xsl:for-each> </p>
![Page 17: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/17.jpg)
Totaling the orders
<xsl:value-of select="sum(//order/@quantity)" />
![Page 18: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/18.jpg)
Returning the Node Names
<xsl:for-each select="//customer">
<xsl:value-of select="name()"/>
</xsl:for-each>
![Page 19: Navigating XML. Overview Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is to address parts of an.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e395503460f94b2b6d9/html5/thumbnails/19.jpg)
Count of Nodes
<xsl:value-of select="count(customer)" />