Home arrow XML arrow Page 2 - 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 - Path Expression Syntax
(Page 2 of 4 )

The basic path expression syntax is similar in style to file-system addressing. If the path starts with the slash ( / ), then it represents an absolute path to the required element. Only the / represents the root element. For the example above, the expression "/" represents <bookstore>. To get all the <book> elements use: 

/book

This will return both <book> elements. To get the <price> elements use:

 /book/price

<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>

If the path starts with // then all elements in the document which fulfill the following criteria are selected. The //B expression selects all <B> tags regardless of their parents.

<root>
          <B/>
          <C/>
          <B/>
          <D>
               <B/>
          </D>
          <C>
               <D>
                    <B/>
                    <B/>
               </D>
          </C>
</root>

The star * selects all elements located by the preceding path. To select all elements under <book> use the /book/* path expression.

<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>


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