Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

XSLT Functions

In Chapter 3, "XPath, XPointer, and XLink," you were introduced to several functions built in to the XPath language. Because XSLT relies on XPath for creating expressions that locate nodes in an XML document, these functions are available for use in your XSLT documents. In addition to these functions, the XSLT language adds a few more. Table 7.3 shows these functions and provides a description and example of using them in XSLT documents.

Table 7.3 XSLT Functions




Returns a node set that contains the current node as its only member. This function exists to help identify the current node when it is different from the context node. In previous examples you have seen that the context node can be represented by the . character. For example, to write out the value of the context node being processed by a template, you can do the following:

<xsl:value-of select="."/>


This code will provide the same result:

<xsl:value-of select="current()"/>


At this point in the template, the current node is the same as the context node. When used within the square brackets of a predicate ([ and ]), however, the current node is often different from the context node. An example of this is shown next:


<xsl:template match="/">
 <xsl:variable name="golferVar" 
<xsl:text>Other Comparable Golfers: </xsl:text> <xsl:for-each select="$golferVar"> <xsl:if test="$golferVar[./@skill=
<xsl:value-of select="$golferVar/name/
</xsl:if> </xsl:for-each> </xsl:template>


The predicate statement ([./@skill = current()/ @skill]) will compare the value of the context node's skill attribute (in this case, the golfer node being looped through) to the golfer node being handled by the template (the current node). This is an example of how the context node changes during a loop. However, the current node is associated with the node being handled by the template and stays the same during the looping process.


Although XSLT makes it easy to transform a single XML document into other structures, what happens if the result tree needs to be created from more than just one XML document? Using the document() function, other XML documents can be pulled into an XSLT document for processing. This can be useful in many situations, including when one XML document contains a presentation structure and another contains the data to be plugged into that structure. Using the document() function, these types of activities can be accomplished relatively easily. An example of using the document() function is shown next:

<xsl:variable name="xmlDoc"


This will load data.xml into the variable named xmlDoc. Referencing elements within the external document can be accomplished in the following manner:

<xsl:value-of select="$xmlDoc//root/data/@id"/>


Although the preceding document() function example targets an external document, this function can also be used to target a node-set within the main XML document. This can be useful when you want to work with a lookup table structure embedded in the XML document.


Aside from providing a string URI value, a node-set can be passed to access the remote document, as shown next:


For the XML document:


        <handicaps href="handicapsWest.xml"/>
        <handicaps href="handicapsEast.xml"/> 


The following XSLT document creates a result tree containing the handicap nodes from the referenced documents:


<xsl:template match="/">
<xsl:template match="handicaps">
<xsl:copy of select="document(@href)//handicap"/>


This function is useful if you are writing an XSLT document that may be processed using different XSLT processors. Before using elements that you know may not be supported, a check can be made to see if the processor does indeed support the element in question. This is helpful when testing for XSLT elements found in a later version of XSLT or in checking for vendor specific elements. The function will return a Boolean value:

<xsl:if test="element-

format-number(number, string,string?)

This function converts numbers to a string using a format pattern supplied in the second parameter. Here are some examples of using this function:


The following function call returns 5,351:



The following function call returns 5351.00:

format-number(5351, "#.00")


The following function call returns 53.5100:

format-number(53.51, "#.0000")


The following function call returns 0053.5100:

format-number(53.51, "0000.0000")


The following function call returns 0053.51:

format-number(53.51, "0000.####")


The following function call returns 53.6:

format-number(53.56, "0.0")

Similar to element-available, although this function checks whether specific functions are supported by the XSLT processor.


This function generates a string that is guaranteed to uniquely identify a node. The same string will always be returned for the same node. The string that is generated will vary from processor to processor and will start with an alphabetic character.


<xsl:attribute name="id">
 <xsl:value-of select="generate-id(.)"/>

This function is used in conjunction with the xsl:key element to return a node-set that has a specific name and value defined by the xsl:key statement. For example, the following code shows how the xsl:key element can define a key:

<xsl:key name="course-city" match="course" use="@city"/>


This key can then be accessed by using the key() function:

<xsl:for-each select="key('course-city', 'Pinetop')"> 

This function returns the value of the system property identified by the name passed as the argument. Three different system properties must be supported by a compliant XSLT processor, including xsl:version, xsl:vendor, and xsl:vendor-url. An example of using this function follows:

<xsl:value-of select="system-property ('xsl:version')"/>

This function returns declarations of unparsed entities in the DTD of the source XML document.


Given the entity declaration:

<!ENTITY clubs SYSTEM "http://www.lottaclubs.com/clubs.txt">


The following code:

<xsl:value-of select="unparsed-entity-uri('clubs')"/>


Would return a value of http://www.lottaclubs.com/clubs.txt.

  • + Share This
  • 🔖 Save To Your Account