Home arrow XML arrow Page 8 - XPath Basics

Be Cool - XML

One of the fundamental constructs for XSL transformations and XML links, XPath is nonetheless one of the lesser lights of the XML universe. However, if you're serious about developing your XML skills, you need to know it inside out - and this tutorial has all you need to get started.

TABLE OF CONTENTS:
  1. XPath Basics
  2. Dog Days
  3. First Steps
  4. Revolving Around An Axis
  5. Proof Of The Pudding
  6. Playing Chicken
  7. Operating With Extreme Caution
  8. Be Cool
  9. The Next Step
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 6
August 15, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: