Home arrow XML arrow Page 3 - Introduction to XPath
XML

Introduction to XPath


According to the W3C, XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer. In this article we will learn about XPath, XPath expressions and how to use XPath in .NET and Java.

Author Info:
By: Mamun Zaman
Rating: 5 stars5 stars5 stars5 stars5 stars / 12
August 15, 2007
TABLE OF CONTENTS:
  1. · Introduction to XPath
  2. · Path Expression Syntax
  3. · Getting Elements
  4. · Using XPath

print this article
SEARCH DEVARTICLES

Introduction to XPath - Getting Elements
(Page 3 of 4 )

You can use indexing of elements to give you results similar to arrays. The /book expression selects both <book> elements. To get the first <book> element use /book[1]. Similarly, for the second <book> element use /book[2] or/book[last()]. The last() function identifies the last element.

/book[last()]

<bookstore>
 
<book>
   
<title id='1'>XPath Tutorial</title>
   
<author>Mamun Zaman</author>
   
<year>2007</year>
   
<price>00.99</price>
  </book>
  <book>
   
<title id='2'>AJAX Tutorial</title>
   
<author>Charles</author>
   
<year>2007</year>
   
<price>03.45</price>
  </book>
</bookstore>

Attributes are specified by the @ prefix. The /book/title/@id expression selects only the attribute as mentioned below.

<bookstore>
 
<book>
   
<title id='1'>XPath Tutorial</title>
   
<author>Mamun Zaman</author>
   
<year>2007</year>
   
<price>00.99</price>
 
</book>
 
<book>
   
<title id='2'>AJAX Tutorial</title>
   
<author>Charles</author>
   
<year>2007</year>
   
<price>03.45</price>
 
</book>
</bookstore>

//B[@id] selects B elements which have the id attribute in the following XML.

  <A>
          <B id = "b1"/>
          <B id = "b2"/>
          <B name = "bname"/>
          <B/>

  </A>

//B[@id='b1'] selects B elements which have the id attribute with a value of b1.


     <A>
          <B id = "b1"/>
          <B name = " xyz "/>
          <B name = "bbb"/>
     </A>

The name() function returns the name of the element. Some other functions for selections are starts-with() and contains(). The //*[starts-with(name(),'B')] expression selects all elements whose names start with 'B'. The//*[contains(name(),'C')] expression selects all elements whose names contain 'C'. The string-length() function returns the length of the input string. The //*[string-length(name())>5] expression selects all the elements whose names are five or more letters long.

Several paths can be combined with the pipe ( | ) separator. The //year | //price expression selects all year and price elements.

<bookstore>
 
<book>
   
<title id='1'>XPath Tutorial</title>
   
<author>Mamun Zaman</author>
   
<year>2007</year>
   
<price>00.99</price>
 
</book>
 
<book>
   
<title id='2'>AJAX Tutorial</title>
   
<author>Charles</author>
    
<year>2007</year>
   
<price>03.45</price>
 
</book>
</bookstore>

To find a node of type text use the text() function. To get the year value use //year/text().

<bookstore>
 
<book>
   
<title id='1'>XPath Tutorial</title>
   
<author>Mamun Zaman</author>
   
<year>2007</year>
   
<price>00.99</price>
 
</book>
 
<book>
   
<title id='2'>AJAX Tutorial</title>
   
<author>Charles</author>
   
<year>2007</year>
   
<price>03.45</price>
 
</book>
</bookstore>

XPath defines four data types: node-sets, strings, numbers and Booleans.

The available operators are:

  • The "/", "//" and "[...]" operators, used in path expressions=.
  • A union operator, "|", which forms the union of two node-sets.
  • Boolean operators "and" and "or", and a function "not()"
  • Arithmetic operators "+", "-", "*", "div" (divide), and "mod"
  • Comparison operators "=", "!=", "<", ">", "<=", ">="

The function library includes:

  • Functions to manipulate strings: concat(), substring(), contains(), substring-before(), substring-after(), translate(), normalize-space(), string-length()
  • Functions to manipulate numbers: sum(), round(), floor(), ceiling()n>
  • Functions to get properties of nodes: name(), local-name(), namespace-uri()
  • Functions to get information about the processing context: position(), last()
  • Type conversion functions: string(), number(), boolean()

Some of the more common and useful functions are detailed in the next section. For a complete description, see the W3C Recommendation document. W3Schools also has some nice XPath articles.


blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials