XML
  Home arrow XML arrow Page 3 - Introduction to XPath
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Introduction to XPath
By: Mamun Zaman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2007-08-15

    Table of Contents:
  • Introduction to XPath
  • Path Expression Syntax
  • Getting Elements
  • Using XPath

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More XML Articles
    More By Mamun Zaman


     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek