Developer

Locate and format XML data with XPath functions

Extracting and formatting XML data does not require Java, Visual Basic, or another language. XPath provides everything you need with built-in functions to get the job done easily.


XPath allows you to quickly locate and extract information from an XML hierarchy, and it offers extended functionality via its built-in functions, which provide an easy way to work with numeric and textual data. Locating data within an XML document doesn't require thorough knowledge of a traditional development language like C# or Java. The combination of XSLT and XPath provides everything you need to locate and format the data accordingly.

The string and number XPath functions are two examples of this functionality. Here, I'll provide an overview of these functions. However, this is by no means meant to be an exhaustive coverage of XPath functions.

Working with numbers
XPath provides numerous functions that make it easy to work with functions. Table A provides a sampling:
Table A
Name
Description
ceiling()
Generates the smallest integer that is not less than the number passed to the function
floor()
Generates the largest integer that is not less than the number passed to the function
number()
The value passed to the function is converted to a number.
round()
The number passed to the function is rounded to the nearest integer.
sum()
A total value is calculated from the set of numeric values (node set) passed to it.
XPath number functions

Let's examine the XML to be used in the examples:
<?xml version="1.0" encoding="ISO-8859-1"?>
<books>
<book type=”hardback”>
<title>Atlas Shrugged</title>
<author>Ayn Rand</author>+
<isbn>0525934189</isbn>

<price>39.95</price>
</book>
<book type=”paperback”>
<title>A Burnt-Out Case</title>
<author>Graham Greene</author>
<isbn>0140185399</isbn>

<price>13.00</price>
</book>
</books>


The number functions may be used to accomplish numerous tasks. For example, the total value of the books (if this were an order) may be easily calculated with the sum() function, like this:
sum(//books/book/price)

This is placed in an accompanying stylesheet to display the results:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<p>Total cost of books = <xsl:value-of select="sum(//books/book/price)"/></p>
<p>Total number of books = <xsl:value-of select="count(//books/book)"/></p>
</xsl:template>
</xsl:stylesheet>


The previous stylesheet may be applied to the sample XML with the following directive:
<?xml-stylesheet href="xslt.xsl" type="text/xsl"?>

This line is added to the XML file to tell the processor what stylesheet to apply to the XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="xslt.xsl" type="text/xsl"?>
<books>
<book type="hardback">
<title>Atlas Shrugged</title>
<author>Ayn Rand</author>
<isbn>0525934189</isbn>
<price>39.95</price>
</book>
<book type="paperback">
<title>A Burnt-Out Case</title>
<author>Graham Greene</author>
<isbn>0140185399</isbn>
<price>13.00</price>
</book>
</books>


The transformation results are displayed in Figure A.

Figure A
Take advantage of simple XPath functions.


The functionality provided by the number functions allows calculations to be accomplished while diving into a programming language like Java or C#. In addition, the XPath text functions allow XML data to be formatted or manipulated as necessary.

Working with text
The XPath string functions are extensive; a partial list is provided in Table B.
Table B
Name Description
concat()
Concentrates two string values
contains()
Returns a Boolean value indicating whether the first value contains the second (true), or not (false)
normalize-space()
Leading and trailing spaces are removed from the value
starts-with()
Returns a Boolean value indicating whether the first value begins with the second value passed in
string()
Converts a value to a string
string-length()
Returns the length of a string value
substring()
Returns a portion of a string. The first parameter is the string, the second is the starting point of the substring, and the third parameter is the ending point of the substring returned.
XPath string functions

The contains() function may be used to determine if an element matches the criteria or not. This example utilizes the contains() function to determine if a certain book has been found:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head><title>Listing B</title></head>
<body>
<h1>XPath functions - Listing B</h1>
<p>Selected book:
<xsl:variable name="title" select="//books/book/title"/>
<xsl:if test="contains($title,'Shrug')">
<xsl:value-of select="$title"/>
</xsl:if>
</p>
</body></html>
</xsl:template>
</xsl:stylesheet>


The result of the previous example is displayed in Figure B.

Figure B
Using XPath string functions


The example combines the power of the XSL if/then directive and the contains() function to locate information. The remaining string functions may be used in a similar fashion. XPath contains Boolean and node-set functions as well, but they are beyond the scope of this article. Look for more about these functions in upcoming articles.

Editor's Picks

Free Newsletters, In your Inbox