XML
  Home arrow XML arrow Page 6 - XPath Basics
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
XML

XPath Basics
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2001-08-15


    Table of Contents:
  • XPath Basics
  • Dog Days
  • First Steps
  • Revolving Around An Axis
  • Proof Of The Pudding
  • Playing Chicken
  • Operating With Extreme Caution
  • Be Cool
  • The Next Step

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    XPath Basics - Playing Chicken
    ( Page 6 of 9 )

    Let's try a slightly more complex example:


    <?xml version="1.0"?>

    <recipe>

    <name>Chicken Tikka</name> <author>Anonymous</author> <date>1 June 1999</date>

    <ingredients>

    <item> <desc>Boneless chicken breasts</desc> <quantity>2</quantity> </item>

    <item> <desc>Chopped onions</desc> <quantity>2</quantity> </item>

    <item> <desc>Ginger</desc> <quantity>1 tsp</quantity> </item>

    <item> <desc>Garlic</desc> <quantity>1 tsp</quantity> </item>

    <item> <desc>Red chili powder</desc> <quantity>1 tsp</quantity> </item>

    <item> <desc>Coriander seeds</desc> <quantity>1 tsp</quantity> </item>

    <item> <desc>Lime juice</desc> <quantity>2 tbsp</quantity> </item>

    <item> <desc>Butter</desc> <quantity>1 tbsp</quantity> </item> </ingredients>

    <servings>3</servings>

    <process> <step>Cut chicken into cubes, wash and apply lime juice and salt</step> <step>Add ginger, garlic, chili, coriander and lime juice in a separate bowl</step> <step>Mix well, and add chicken to marinate for 3-4 hours</step> <step>Place chicken pieces on skewers and barbeque</step> <step>Remove, apply butter, and barbeque again until meat is tender</step> <step>Garnish with lemon and chopped onions</step> </process>

    </recipe>


    Now, if I wanted to get to the third ingredient, I would use the path


    /recipe/ingredients/item[3]/desc/text()


    whish references the text string


    Ginger


    Note the predicate used to get the third item in the list.

    If I needed to get the number of servings, I could use


    /recipe/servings/text()


    or


    //servings/text()


    The // shortcut will select elements of that name anywhere below the current context node, and is equivalent to the "descendant-or-self" axis. So the path


    //item


    would select all the "item" nodes in the document, while the path


    //item[7]/quantity


    would reference the "quantity" element under the seventh "item" element and the path


    //item[7]/quantity/text()


    would reference the text node


    2 tbsp


    If I wanted to get really funky, I could use a different predicate to identify the appropriate node - the paths


    //item[7]/quantity/text()


    and


    //item[desc/text()='Lime juice']/quantity/text()


    are equivalent.

    More examples of this nature are available in the XPath specification at http://www.w3.org/TR/xpath.html

    This article copyright Melonfire 2001. All rights reserved.

     
     
    >>> More XML Articles          >>> More By Vikram Vaswani, (c) Melonfire
     

       

    XML ARTICLES

    - Flex Array Collection Sort and Filtering
    - The Flex Tree Control
    - Flex List Controls
    - Working with Flex and Datagrids
    - How to Set Up Podcasting and Vodcasting
    - Creating an RSS Reader Application
    - Building an RSS File
    - An Introduction to XUL Part 6
    - An Introduction to XUL Part 5
    - An Introduction to XUL Part 4
    - An Introduction to XUL Part 3
    - An Introduction to XUL Part 2
    - An Introduction to XUL Part 1
    - XML Matters: Practical XML Data Design and M...
    - Practical XML Data Design and Manipulation f...





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