Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

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

Refine XPath results using predicate matching

Brett D. McLaughlin, Sr., Author and Editor, O'Reilly Media, Inc.
Photo of Brett McLaughlin
Brett McLaughlin is a bestselling and award-winning non-fiction author. His books on computer programming, home theater, and analysis and design have sold in excess of 100,000 copies. He has been writing, editing, and producing technical books for nearly a decade, and is as comfortable in front of a word processor as he is behind a guitar, chasing his two sons around the house, or laughing at reruns of Arrested Development with his wife. His last book, Head First Object Oriented Analysis and Design, won the 2007 Jolt Technical Book award. His classic Java and XML remains one of the definitive works on using XML technologies in the Java language.

Summary:  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.

Date:  17 Jun 2008
Level:  Intermediate PDF:  A4 and Letter (1286 KB)Get Adobe® Reader®

Activity:  14538 views
Comments:  

Before you start

Learn what to expect from this tutorial and how to get the most out of it.

About this tutorial

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.


Objectives

Frequently used acronyms

  • API: application programming interface
  • HTML: Hypertext Markup Language
  • URI: Uniform Resource Identifier
  • W3C: World Wide Web Consortium
  • XHTML: Extensible Hypertext Markup Language
  • XML: Extensible Markup Language
  • XSL: Extensible Stylesheet Language
  • XSLT: XSL Transformations

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.


Prerequisites

The most important prerequisite to this tutorial is that you thoroughly read and work through Part 1 (see Resources). 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 an understanding of nodes, but is not required. If you want to read up on the DOM, visit Resources to find 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 Resources in the tutorial.

1 of 12 | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML, Java technology
ArticleID=313392
TutorialTitle=Locate specific sections of your XML documents with XPath, Part 2
publish-date=06172008
author1-email=brett@newInstance.com
author1-email-cc=dwxed@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.