Developer

XPath functions offer better translations and less programming

When it comes to handling data in your XSL template, XPath functions provide a simple way to get the information you need. See what these functions can do for you.


By Brian Schaffner

Using XML’s Path Language (XPath) functions in your XSLT templates can help reduce the amount of programming that you need to do when it comes time to translate your data. Let’s run through some of the same functions you can use to gather and manipulate quantities and improve your XSL translations.

Count(node-set)
You can use the count() function to determine the number of nodes in a template, which can be useful for processing XML data that contains a dynamic set of data. It's also useful for creating summary data from XML data. Suppose you want to perform a translation that shows the number of items on a customer's order. Here's an example of how you might use the count() function to achieve this:
<xsl:template match="OrderRecord">
  <ItemCount><xsl:value-of select='count(/OrderRecord/Items/LineItem)'/></ItemCount>
</xsl:template>


Not(boolean)
The not() function returns true or false depending on the value of the Boolean expression. Not() will return the inverse value of the Boolean value. For example, not(true()) returns false and not(false()) returns true. This function is useful for performing logic when the condition being tested is a false condition rather than a true condition. We can use this function to change the behavior of our count() example above. In this case, when there are no LineItems, we won't put the ItemCount element in the resulting XML:
<xsl:template match="CustomerRecord">
  <xsl:if test="not(count(/CustomerRecord/RecordData) = 0)">
    <ItemCount><xsl:value-of select='count(/CustomerRecord/RecordData)'/></ItemCount>
  </xsl:if>
</xsl:template>


Sum(node-set)
The sum() function creates a summary value based on the node-set provided. This function can be used to sum the values of nodes such as prices, quantities, and weights. Suppose your translation needs to determine the total amount for an order based on the line-item subtotals. The following example illustrates how to do this:
<xsl:template match="OrderRecord">
  <TotalPrice><xsl:value-of select='sum(/OrderRecord/LineItems/Item/Subtotal)'/></TotalPrice>
</xsl:template>


Round(number)
The round() function is used to round off floating-point numbers to integer values. This function is useful when the floating-point value or values need to be converted to integers because of the target system they are going into or because you need to round off to a different precision (such as converting three-decimal currency to two-decimal currency). Suppose you sell transactions for fractions of a penny, but your invoicing system must bill customers using dollars and whole cents. In this case, you will need to round off the values in order to put them into the invoicing system. To keep from losing any pennies, we'll sum the values first and then round off as a final step. The following example illustrates this:
<xsl:template match="OrderRecord">
  <TotalPrice><xsl:value-of select='round(sum(/OrderRecord/LineItems/Item/Subtotal) * 100) div
  100'/></TotalPrice>
</xsl:template>


Summary
In this article, we looked at four XPath functions that you can use in your XSLT templates. XPath functions provide a nice set of functionality for creating complex transformations. Using these functions can reduce the amount of programming required when a system receives the XML data.

 

Editor's Picks

Free Newsletters, In your Inbox