Skip to main content


developerWorks  >  XML | Java technology  >

Locate specific sections of your XML documents with XPath, Part 2

Refine XPath results using predicate matching

developerWorks

Level: Intermediate

Brett D. McLaughlin, Sr. (brett@newInstance.com), Author and Editor, O'Reilly Media, Inc.

17 Jun 2008

Register now or sign in using your IBM ID and password.

Part 1 of this tutorial gave you a foundational understanding of XPath. Using slash notation, wildcards, unions, and simple text, you learned how to locate elements and attributes anywhere within an XML document. However, sometimes you need more than just matching based on the name of a node. Predicates give you advanced and refined searching capabilities, allowing you to evaluate the values of attributes and the parent and child nodes of a targeted element. Rather than find a wider node set and refine or filter that set programmatically, you can add predicates to your XPaths to find exactly the nodes you want.

In this tutorial

Other tutorials in this series

This tutorial details the XPath specification, which allows you to specify particular sections of an XML document using a directory-like syntax. You'll learn the syntax of XPath, and you'll work with tools that let you experiment with XPath. By the time you complete this tutorial, you'll be well beyond the basics of XPath, and you'll understand predicates, more complex matching, and how to use XPath in your own applications.

Part 2 of this tutorial focuses on using predicates and predicate matching in your XPaths. It covers the various selectors XPath provides for being as specific or generic in a search as you like. It also shows you how to build complex predicates into a SQL-like set of queries, all using only standard XPath syntax.

You'll begin to understand the ideal applications of XPath, and you'll know when XPath is best used alongside another XML technology such as XQuery. Finally, you'll see how XPath deals with data types, particularly when you perform comparisons of attribute values to numeric values.


Objectives

  • Set up your environment for the examples

  • Be more selective with predicates

  • Select nodes by their position

  • Compare and filter nodes based on the values of attribute nodes

  • Compare the text of element nodes to values

  • Use the XPath axes to navigate

  • Build SQL-like joins with predicates

  • Understand the ideal applications of XPath, and when to add other technologies such as XQuery

Prerequisites

The most important prerequisite to this tutorial is that you thoroughly read and work through Part 1. That tutorial covers all the basics of XPath, and it includes a detailed discussion of nodes and the piece-by-piece evaluation that XPaths are handled with. Make sure you're familiar with all of that before you start Part 2.

Additionally, this tutorial is written for XML document authors and programmers. You should be familiar with and comfortable reading, writing, and manipulating XML. You should also be familiar with XML concepts, including the following:

  • Elements
  • Attributes
  • Text
  • The root element

Familiarity with the Document Object Model (DOM) is helpful for understanding nodes, but is not required. If you want to read up on the DOM, visit the Resources in the tutorial for several relevant links. You should also make sure you're comfortable with the section on nodes from Part 1 of this tutorial.

This tutorial will mention several other APIs and specifications as well, including XSL, XSLT, and XPath. Knowledge of any of these is helpful, but not required. For more information on any of these, consult the Resources in the tutorial.


System requirements

You will need JavaScript enabled in your browser.

You'll work with an XML document throughout this tutorial. You'll need to have this document accessible on your machine, and you'll need to be familiar with the basic structure of the document. Additionally, you'll need a tool that executes your XPath locations and gives you feedback on what you've selected. The tutorial explains how to get all of this working on your personal environment so you can follow along with the tutorial examples.

Tools to evaluate XPath locations are still specific to each operating system. Some of the nicer tools that download as EXE files and run on Microsoft® Windows® won't work on Mac OS X. Similarly, the tools to work with XPath on Mac OS X won't run on Windows. While you can use Java™ programs and classes to create a system-independent means to work with XPath, this tutorial focuses on XPath rather than any particular programming language.

  • To work with XPath and XML documents on the Windows platform, download and install Stylus Studio 2008 XML.
  • To enable easy XPath location evaluation on Mac OS X, download and install AquaPath .


Duration

1 hour





Formats

html, pdf


Share this....

digg Digg this story del.icio.us del.icio.us Slashdot Slashdot it!


Back to top


Document options

Document options requiring JavaScript are not displayed


My developerWorks needs you!

Connect to your technical community