1. Home
  2. Computing & Technology
  3. Web Design / HTML

Ecommerce Functions in XPath
Use XPath with Your XSLT to Create Ecommerce Sites

By Jennifer Kyrnin, About.com Guide

There are many advanced functions of XPath to allow you to manipulate your XSLT. These functions help you reduce the amount of programming you need to do after transforming the data. These functions will help you create ecommerce documents and manipulate the data.

XPath count() Function

The count function allows you to work with data that has dynamic information. You can use it to select elements with a specific number of child elements, or perform a translation to count the number of elements.

For example, in an ecommerce function, you might want a count of all the items ordered.

<xsl:template match="order">
  <Number_Ordered><xsl:value-of select='count(/Order/Item)' /></Number_Ordered>
</xsl:template>

XPath sum() Function

Using the sum function, you can add values in your XML document. This is especially useful in ecommerce, such as when you might want to add subtotals to get a grand total.

For example, if your first document has multiple <subtotal> entries, you can get a grand total using the sum function:

<xsl:template match="order">
  <Grand_Total><xsl:value-of select='sum(/Order/SubTotal)' /></Grand_Total>
</xsl:template>

XPath round() Function

The round function allows you to round floating point numbers into integers. This is especially useful when you are selling items for fractions of a penny. You can only charge full pennies, so to get to that you must round the numbers off to the nearest penny.

For example, if the sum transformation we used in the previous example were of subtotals a fraction of a penny, you would need to sum them, then multiply by 100 so as not to lose any penny, then round to the nearest integer value, and divide again by 100 to get the value to the penny.

<xsl:template match="order">
  <Grand_Total><xsl:value-of select='round(sum(/Order/SubTotal) * 100) div 100' /></Grand_Total>
</xsl:template>

XML is a natural language for ecommerce, and XPath with XSLT provides many ways to manipulate your XML documents to help manage your ecommerce tools and data.

Previous Features

Explore Web Design / HTML
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Web Design / HTML
  4. XML
  5. Specifications
  6. XPath
  7. Ecommerce Functions in XPath>

©2009 About.com, a part of The New York Times Company.

All rights reserved.