XPath

XPath is a standard XML Query Language, used in XSLT, XSD, XPointer etc and with implementations for most platforms.


Introduction

XPath (XML Path Language) is a language for selecting nodes from within an XML document. Version 1.0 of the XPath standard was released in 1999, and is now supported in most languages and platforms.
Version 2.0 of the standard was released in 2007 in an effort to share more functionality between the XSLT and XQuery standards. Version 2.0 is backwardly compatible with 1.0 and adds a number new operators including looping, set manipulation, conditional expression, and aggregate functions.
 

Examples

Sample Data

<?xml version="1.0" encoding="utf-8"?>
<Catalog>
    <Album artist="The Last Shadow Puppets" title="The Age Of The Understatement">
        <Track rating="4" length="P3M7S">The Age Of The Understatement</Track>
        <Track rating="3" length="P2M18S">Standing Next To Me</Track>
        <Track rating="5" length="P2M26S">Calm Like You</Track>
        <Track rating="3" length="P3M38S">Separate and Ever Deadly</Track>
        <Track rating="2" length="P2M37S">The Chamber</Track>
        <Track rating="3" length="P2M44S">Only The Truth</Track>
    </Album>
    <Album artist="Kings Of Leon" title="Because Of The Times">
        <Track rating="4" length="P7M10S">Knocked Up</Track>
        <Track rating="2" length="P2M57S">Charmer</Track>
        <Track rating="3" length="P3M21S">On Call</Track>
        <Track rating="4" length="P3M09S">McFearless</Track>
        <Track rating="1" length="P3M59S">Black Thumbnail</Track>
    </Album>
</Catalog>

Sample XPath Expressions

XPath Expression Results
/Catalog/Album
(Click to enlarge)
select all the albums
/Catalog/Album/@artist
(Click to enlarge)
select the artist attribute for all albums
/Catalog/Album/Track
(Click to enlarge)
select all the tracks in all albums
/Catalog/Album[@artist="Kings Of Leon"]
(Click to enlarge)
select all albums by the Kings Of Leon
/Catalog/Album[@artist="Kings Of Leon"]/Track
(Click to enlarge)
select all tracks in all albums by the Kings Of Leon
/Catalog/Album[2]
(Click to enlarge)
select the second album
/Catalog/Album/Track[@rating>2]
(Click to enlarge)
selects all tracks with a rating greater than 2
/Catalog/Album/Track/text()
(Click to enlarge)
selects the text for all the tracks

Resources

Standards

Tutorials

Tools

 Liquid XML XPath Expression Builder (Included in the free Comunity Edition)

Implementations

Java

  • Jaxen is an Open Source XPath implementation supporting (embedded by) multiple XML parsers (XOM, Dom4J, JDom).
  • Saxon supports XPath 1.0 and XPath 2.0 (as well as XSLT 1.0, XSLT 2.0, and XQuery 1.0)

C++

  • TinyXpath a small footprint XPath syntax decoder, written in C++.
  • Apache Xalan-C++ is a robust implementation of the W3C Recommendations for XSL Transformations (XSLT) and the XML Path Language (XPath).
  • XQilla is an XQuery and XPath 2.0 Open Source library, implemented on top of the Xerces-C library
  • libxml2 an XML C parser and toolkit developed for the Gnome project.

 

Comments

Jackie Sprott
Jackie Sprott
XML Technologies Consultant
Article rating:
Your rating:
Version: 12
Versions
Last edited: Aug 3, 2008 2:34 AM.

Categories

Based on community consensus.

Activity for this knol

This week:

17pageviews

Totals:

689pageviews