XML technologies / XML / Python & XML / Querying XML with XPath / Arithmetic XPath operators
(Excerpt from "Python & XML" by Christopher A. Jones & Fred L. Drake, Jr.)
Besides selecting elements with location paths, XPath supports data manipulation. Numeric parts of an XML document can be added, divided, subtracted and multiplied. Strings can also be tested for equality.
XPath provides arithmetic operators for use in expressions. This is useful in XSL transformations that total a product list or apply discounts to prices for display in HTML. The operators available in XPath are +, -, *, div and mod (for addition, subtraction, multiplication, division and modulo, respectively). There are also functions such as sum, for summing sets of numbers and performing other tasks. Functions are covered in Transforming XML with XSLT.
Suppose an XML file lists products to display in another application, such as your website, with a 20% discount on all sale prices. XPath arithmetic operators can solve this problem. Consider the XML source document, products.xml, in the following example.
Example: products.xml
<?xml version="1.0" encoding="UTF-8"?>
<products>
<item name="Schale" price="19.95"/>
<item name="Spaten" price="4.95"/>
<item name="Kraftmixer" price="149.95"/>
<item name="Kochhut" price="39.95"/>
</products>To give a blanket 20% discount on all products, use XPath in an XSLT document. The XSLT in the next example, products.xsl, solves the problem.
Example: products.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="item">
<p><b>Artikel:</b> <xsl:value-of select="@name"/>Originalpreis: <xsl:value-of select="@price"/>, Unser Preis: <xsl:value-of select="@price * 0.8"/></p>
</xsl:template>
</xsl:stylesheet>The numeric XPath expressions appear in the elements xsl:value-of. The discount is applied by multiplying the value of the price attribute by 0.8. You can run the transformation with the tool 4xslt (see XSLT processors):
$ 4xslt.bat products.xml products.xsl
<html>
<body>
<p><b>Artikel:</b> Schale Originalpreis: 19.95, Unser Preis: 15.96</p>
<p><b>Artikel:</b> Spaten Originalpreis: 4.95, Unser Preis: 3.96</p>
<p><b>Artikel:</b> Kraftmixer Originalpreis: 149.95, Unser Preis: 119.96</p>
<p><b>Artikel:</b> Kochhut Originalpreis: 39.95, Unser Preis: 31.96</p>
</body>
</html>The operators div and mod work like the other operators. For example, @price div 2 divides all selected prices by 2.
Copyright © 2002 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Python & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top