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.
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:
<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>
Be Cool - Elmore Leonard;
Mystic River - Dennis Lehane;
Hit List - Lawrence Block;
Silent
Joe - T. Jefferson Parker;
The Travel Detective - Peter Greenberg;