XML
  Home arrow XML arrow Page 8 - 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 - Be Cool
    ( Page 8 of 9 )

    In addition to these operators, XPath also comes with some useful arithmetic and non-arithmetic functions - here's a list of the most useful ones::


    Function What It Does ----------------------------------------------------------------- name() returns the name of the element count (collection) returns the number of nodes in the collection last() returns the number of the last node in the current collection position() returns the position of the context node in the current collection sum (collection) returns the sum of the node values in the collection round (num) returns a rounded integer value concat (str1, str2, ...) concatenates its arguments into a single string contains (str, substr) returns true if string contains substring substring (str, start, len) returns a substring of length (len) from position (start) string-length (str) returns the number of characters in the string


    I'll illustrate this with reference to the following XML document:


    <?xml version="1.0"?> <bookstore owner="me" location="East Fifty-Third Street"> <title>Be Cool</title> <author>Elmore Leonard</author> <price>7.97</price> <quantity>150</quantity> <title>Mystic River</title> <author>Dennis Lehane</author> <price>25.00</price> <quantity>86</quantity> <title>Hit List</title> <author>Lawrence Block</author> <price>23.76</price> <quantity>26</quantity> <title>Silent Joe</title> <author>T. Jefferson Parker</author> <price>24.99</price> <quantity>268</quantity> <title>The Travel Detective</title> <author>Peter Greenberg</author> <price>34.87</price> <quantity>9</quantity> </bookstore>


    Here are some examples of how these functions might be used.

    The rule


    <xsl:template match="/"> Total number of items in stock = <xsl:value-of select="sum(//quantity)"/> </xsl:template>


    returns the sum of all the available quantities


    Total number of items in stock = 539


    The rule


    <xsl:template match="/"> Total number of unique titles available for sale = <xsl:value-of select="count(//title)"/> </xsl:template>


    returns the number of titles available


    Total number of unique titles available for sale = 5


    The rule


    <xsl:template match="/"> <xsl:value-of select="//title[last()]"/> </xsl:template>


    returns the last title in the collection.


    The Travel Detective


    The rule


    <xsl:template match="/"> Total value of inventory = <xsl:value-of select="round((//quantity[1] * //price[1]) + (//quantity[2] * //price[2]) + (//quantity[3] * //price[3]) + (//quantity[4] * //price[4]) + (//quantity[5] * //price[5]))"/> </xsl:template>


    multiplies price by quantity and adds it all up.


    Total value of inventory = 10974


    The rule


    <xsl:template match="/"> There are <xsl:value-of select="string-length(//title[1])"/> characters in <xsl:value-of select="//title[1]"/> </xsl:template>


    returns the length of the selected text node


    There are 7 characters in Be Cool


    The rule


    <xsl:template match="/bookstore"> <xsl:for-each select="title"> <xsl:value-of select="self::title" /> - <xsl:value-of select="following-sibling::author" />; </xsl:for-each> </xsl:template>


    returns a list of title-author combinations


    Be Cool - Elmore Leonard; Mystic River - Dennis Lehane; Hit List - Lawrence Block; Silent Joe - T. Jefferson Parker; The Travel Detective - Peter Greenberg;


    And finally, the rule


    <xsl:template match="/"> <xsl:value-of select="name(//*[@owner])"/> </xsl:template>


    first selects any element containing the attribute["owner"] and then returns the element name.


    bookstore


    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