More XPath functions

A few weeks ago, we discussed some of the string functions available in XSL templates via the XPath language. Using XPath functions in your templates can help reduce the amount of programming you need to do with the resulting translation. This week, we'll look at some functions that you can use to improve your XSL translations.

 

Count(node-set)

The count() function can be used to determine the number of nodes in a template. This 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() functions 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 you have need to be converted 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 accordingly. 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

This week, we've shown you more XPath functions you can use in your XSL 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.

 

Hosted by www.Geocities.ws

1