XSLT Tutorial
XPath: Functions
XSLT stylesheets can take advantage of large list of functions. The following functions are
just a few examples that can be used in XPath queries.
The first part of the XML document that we are going to using in our examples is shown below.
A link to the full text of this document is in Worksheet 2 (next page).
<books>
<book>
<name link="http://www.amazon.com/exec/obidos/ASIN/0789722429/vbxml">XML by Example</name>
<author>Benoit Marchal</author>
<listprice>24.99</listprice>
<price>17.49</price>
<review>4.5</review>
<publish>QUE</publish>
</book>
<book>
<name link="http://www.amazon.com/exec/obidos/ASIN/1861003110/vbxml">Professional XML</name>
<author>Mark Birbeck, Michael Kay, stev Livingstone</author>
<listprice>49.99</listprice>
<price>34.99</price>
<review>4</review>
<publish>Wrox</publish>
</book>
...
</books>
count
The count function takes a node-set, which returns a number of the
nodes present in that node-set
In the following example we are looking for the number of books with
a review of 3.5 and 4:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<p>Review of 3.5 =
<xsl:value-of
select="count(books/book[review=3.5])"/></p>
<p>Review of 4 =
<xsl:value-of
select="count(books/book[review=4])"/></p>
</xsl:template>
</xsl:stylesheet>
The output:
Review of 3.5 = 1
Review of 4 = 2
NOTE: If you are running the
msxml3 parser (October release) and have not run the xmlinst.exe to
instruct the parser to run in replace mode, this count example will not
run. For more information on the msxml parser see MSXML Parsers .
To run this example yourself,
Open Worksheet 2 - Simple XPath example
|