XML - Managing Data Exchange/C.5

XML Style Sheet
Similar to the layout we visited in chapter 3, this style sheet lists each hotel in the XML document by country. But this time some new features have been added. Price range elements have been included, each hotel’s amenities are now listed, and the population of each country has been formatted to improve readability.

XSLT Functions
When formatting XML output, there may be times when you might want to massage the data or get certain values for display. Placed within the XML style sheet, functions can be used to manipulate data during the transformation. We saw an example of this in chapter 3 when displaying the number of hotels within a city.



In order to make the population a little easier to read in our style sheet, we need to separate the digits into groups of threes and add commas. To do this we use the format-number function. The purpose of this function is to convert a numeric value into a string using specified patterns that control the number of leading zeroes, separator between thousands, etc. The basic syntax of this function is as follows:

format-number (number, pattern)


 * number is the numeric value to be formatted
 * pattern is a string that lays out the general representation of a number. Each character in the string represents either a digit from number or some special punctuation such as a comma or minus sign.

In order to display with the digits grouped by three’s using commas as the separator, the format-number function was used in line {51} of our XML style sheet.

The following are the characters and their meanings used to represent the number format when using the format-number function within a style sheet:

There are numerous other functions that you may find use for within a style sheet. The following are some additional numeric based functions that are useful when handling numbers within a stylesheet.

       World Renowned Hotels  H2 {TEXT-ALIGN:CENTER;} .blueBackground {BACKGROUND-COLOR:LIGHTBLUE; COLOR:BLACK; TEXT-ALIGN:LEFT; BORDER-WIDTH:0px; WIDTH:90%;} .blackBackground {BACKGROUND-COLOR:BLACK; TEXT-ALIGN:LEFT; COLOR:WHITE; FONT-SIZE:12pt; BORDER-WIDTH:2px;}    * * * Hotels Listings by Country * * * <xsl:apply-templates select="tourGuide"/> </BODY> </HTML> </xsl:template> <xsl:template match="tourGuide"> <xsl:for-each select="city"> </TD> </TR> <TR> <TD colspan="2"> <font style="font-size:medium">Hotels: <xsl:for-each select="hotel"> <BR/> </xsl:for-each> </TD> </TR> </TABLE> </xsl:for-each> </xsl:template> </xsl:stylesheet> Figure 5-5 XML stylesheet – amenity.xsl

In the XML stylesheet, you will see two additional functions position and last. The position function returns the position of the current context node in the current context node-set. The last function returns the index number of the last node in the context. To list each amenity for each hotel, the transformation checks to see if the current position of the node is different than the position of the last context node in the set. If this returns true, then a comma is added as the amenities are listed.

Although this stylesheet builds upon the one we saw in chapter 3, notice the use of embedded tables to further specify the layout of the data being conveyed. Values and their indicators have been separated into table cells, allowing for a cleaner look and feel.