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.Next: The Next Step >>
More XML Articles
More By Vikram Vaswani, (c) Melonfire