Chapter 1. Introduction to XML integration with DB2 for i5/OS 9
Figure 1-2 Sample graphical representation of XSD
1.1.4 XPath and Location Path
XPath is one of the corner stones of the World Wide Web Consortium (W3C) Extensible
Stylesheet Language Transformation (XSLT) standard. It is a language that describes how to
locate specific elements (attributes, comments, processing instructions, and so on) in an XML
document. XPath allows you to locate specific content within an XML document.
An XPath expression is used to navigate the XML hierarchy and return parts of the XML
documents. Generally, an XPath expression can return one of the following data types:
򐂰 Node set
򐂰 Boolean
򐂰 Number
򐂰 String
Brand
Reference : Amount
Reference : Returns Returns Reference : Currency
Returns
Reference : NameBrandReference : Brand
Reference : Amount
Reference : Sales Sales Reference : Currency
Sales
Amount
Amount
Type xsd:string
Currency
Currency
Type
xsd:string
Amount
Amount
Type
xsd:string
Currency
Currency
Type xsd:string
Reference : Name
Address
Reference : PostalCode
Reference : Address Address Reference : Street
Reference : City
Name
Name
Type
xsd:string
PostalCode
PostalCode
Type
xsd:string
Street
Street
Type
xsd:string
City
City
Type xsd:string
StoreSales
Name
Name
Type xsd:string
Data Type : xsd:string
Attribute List
10 The Ins and Outs of XML and DB2 for i5/OS
In most cases, we use XPath expressions to return node sets. A node set can be empty or
contain single node or many nodes. There are several kinds of nodes:
򐂰 Element
򐂰 Attribute
򐂰 Tex t
򐂰 Namespace
򐂰 Processing-instruction
򐂰 Comment
򐂰 Document (root)
XPath treats an XML document as a logical ordered tree of nodes. The root of the tree is
called the
document node (or root node). Do not confuse the XPath root node with XML root
element. In XPath, the root node is represented by a forward slash (/).
An XPath expression contains one or more location steps, separated by slashes. Each
location step has the following form:
axis-name::node-test[predicate]
The XPath axis contains a part of the document, defined from the perspective of the “context
node”. The XPath specification defines a number of different axes:
򐂰 ancestor: Holds all ancestors of the current node
򐂰 ancestor-or-self: Holds all ancestors of the current node and the current node itself
򐂰 attribute: Holds all attributes of the current node
򐂰 child: Holds all children of the current node
򐂰 descendant: Holds all descendants of the current node
򐂰 descendant-or-self: Holds all descendants of the current node and the current node itself
򐂰 following: Holds everything in the document after the closing tag of the current node
򐂰 following-sibling: Holds all siblings after the current node
򐂰 namespace: Holds all namespace nodes of the current node
򐂰 parent: Holds the parent of the current node
򐂰 preceding: Holds everything in the document that is before the start tag of the current
node
򐂰 preceding-sibling: Holds all siblings before the current node
򐂰 self: Selects the current node
The
node test makes a selection from the nodes on that axis. The two most useful node tests
are:
򐂰 node(): Selects any type of node
򐂰 text(): Selects a text node
By adding
predicates, it is possible to select a subset from these nodes. A predicate contains
an XPath expression. If the expression in the predicate returns true, the node remains in the
selected set; otherwise it is removed.
XPath expressions are extensively used in many scenarios and examples that are discussed
in this redbook. We wrote a simple Java utility that can be helpful in testing the correctness of
XPath expressions. Example 1-6 shows the source listing of our XPathTester program.
Chapter 1. Introduction to XML integration with DB2 for i5/OS 11
Example 1-6 XPathTester.java
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.xpath.*;
import org.w3c.dom.NodeList;
public class XPathTester {
private Document document;
private String query;
public XPathTester(String filename) {
try {
InputSource inputSource = new InputSource(filename); 1
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
do {
System.out.print("XPath> ");
query = in.readLine(); 2
try {
XPath xpath = XPathFactory.newInstance().newXPath(); 3
NodeList nodes = (NodeList) xpath.evaluate(query,
inputSource, XPathConstants.NODESET); 4
if (nodes == null) {
System.out
.println("XPath query must return a node-set");
} else {
for (int i = 0; i < nodes.getLength(); i++) { 5
System.out.println("Name: "
+ nodes.item(i).getNodeName());
System.out.println("Value: "
+ nodes.item(i).getNodeValue());
}
}
} catch (XPathExpressionException e) {
System.out.println("Invalid XPath query: " + query);
}
} while (!(query.equals("quit")));
} catch (IOException e) {
System.err.println("Could not load '" + filename + "': "
+ e.toString());
}
}

Get The Ins and Outs of XML and DB2 for i5/OS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.