16.10.2015 Views

Getting Started with DB2 Express-C

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

268 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>DB2</strong> <strong>Express</strong>-C<br />

A quick way to learn XPath is to compare it to the change directory (cd) command<br />

in MS-DOS or Linux/UNIX. Using the cd command, you traverse a directory tree as<br />

follows:<br />

cd /directory1/directory2/…<br />

Similarly, in XPath you use slashes to go from one element to another <strong>with</strong>in the XML<br />

document. For example, using the document in Listing 15.1 in XPath you could retrieve<br />

the names of all employees using this query:<br />

/dept/employee/name<br />

15.3.2.1 XPath expressions<br />

XPath expressions use fully qualified paths to specify elements and attributes. An “@” sign<br />

is used to specify an attribute. To retrieve only the value (text node) of an element, use the<br />

text() function. Table 15.2 shows XPath queries and the corresponding results using the<br />

XML document from Listing 15.1.<br />

XPath<br />

Result<br />

/dept/@bldg 101<br />

/dept/employee/@id 901<br />

902<br />

/dept/employee/name<br />

/dept/employee/name/text()<br />

Peter Pan<br />

John Doe<br />

Peter Pan<br />

John Doe<br />

Table 15.2 – XPath expression examples<br />

15.3.2.2 XPath wildcards<br />

There are two main wildcards in XPath:<br />

• “*” matches any tag name<br />

• “//” is the “descendent-or-self” wildcard<br />

Table 15.3 provides more examples using the XML document from Listing 15.1<br />

XPath<br />

Result<br />

/dept/employee/*/text()<br />

John Doe<br />

408 555 1212<br />

344<br />

Peter Pan

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!