30.08.2014 Views

url - Universität zu Lübeck

url - Universität zu Lübeck

url - Universität zu Lübeck

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

22 CHAPTER 2. FUNDAMENTALS<br />

2.3 XML Query Languages<br />

The DOM API provides a universal way of accessing an XML document by a programming<br />

language. However, the explicit navigation to nodes by a programming<br />

language is not very comfortable. For instance, the following Java code selects all<br />

elements of items that have the value ’MP3 Player’ in its description.<br />

1 Document doc = (new DOMBuilder ( ) ) . build (XMLSource) ;<br />

2 Element root = doc . getRootElement ( ) ;<br />

3 Element description = root . getChild ( ” description ” )<br />

4 i f ( description != null ) {<br />

5 Element text = description . getChild ( ” text ” )<br />

6 i f ( text != null ) {<br />

7 Element emph = text . getChild ( ”emph” )<br />

8 i f (emph != null && emph. getText ( ) . equals ( ”MP3 Player ” ) ) { // found<br />

9 Element name = root . getChild ( ”name” ) ;<br />

10 return name;<br />

11 }<br />

12 }<br />

13 }<br />

14 return null ; // not found<br />

Figure 2.9: A Java program for accessing the DOM-tree<br />

As one can see quickly the code is not intuitive and descriptive. Rather than defining<br />

which nodes are selected, the programmer has to express how to select them.<br />

For more complex queries with more than one value comparison the code will<br />

quickly become unmanageable. For this reason a more descriptive and compact<br />

query language for XML is needed.<br />

2.3.1 XPath<br />

One common approach is XPath [126] a language for expressing navigational<br />

steps and conditions for selecting nodes (elements and attributes) in an XML document.<br />

XPath 1.0 was introduced in 1999 by the W3C with a definition for the<br />

syntax and the semantics. Specific XPath implementations are offered by third<br />

parties like [4, 7, 101, 108] or are part of a database management system, e.g.<br />

[8, 55].<br />

The following expression motivates XPath: It selects the same nodes as the previous<br />

Java code but is much more compact:<br />

1 \item\name[ . . \ description \ text \emph= ’MP3 Player ’ ]<br />

XPath is a navigation language for selecting parts of an XML document modeled<br />

as a tree of nodes. XPath discriminates between seven types of nodes: The documents<br />

root-node, normal elements, text nodes, attributes and, less important,<br />

comment-nodes, processing instructions, and XML Namespaces .

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

Saved successfully!

Ooh no, something went wrong!