1. Home
  2. Computing & Technology
  3. Web Design / HTML
Ecommerce Functions in XPath

 Related Resources
• Introduction to XPath
• XPath Links
• XPointer Links
• XLink Links
 
 From Other Guides
• XML and Ecommerce
 
 Stay Up-to-Date
  with Web Design
  Email:
  

By Jennifer Kyrnin

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.

count()
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>

sum()
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>

round()
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

More from About.com

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

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

All rights reserved.