BASICS

ADMINISTRATION

DEVELOPMENT

SUPER ADMIN

  LAST UPDATED
5/22/2013

XSL Functions

XSL functions allow developers to create stylesheet functions that can be called from any XPath expression used in the stylesheet. The default syntax for an xsl:function is as follows:

<xsl:function
      name= qname
      as=sequence-type
      override = "yes" | "no"

<!-- Content: (xsl:param, sequence-constructor) -->
</xsl:function>

Example:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "http://commons.omniupdate.com/dtd/
standard.dtd">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/
XSL/Transform" xmlns:ou="http://omniupdate.com/XSL/Variables">
<xsl:template match="/">
      <xsl:copy-of select="ou:sample_function()"/>
</xsl:template>
 
<!-- function Definition -->
<xsl:function
      name= "ou:sample_function"
      as="xs:string">
<!--
Sequence processing for the function.
-->
</xsl:function>
</xsl:stylesheet>

Attribute: name (mandatory): Defines the name of the function. The name must have a namespace prefix. This will ensure that the name does not clash with the names of functions in the standard function library. XSLT specification has reserved a pre-defined namespace that cannot be used for the names of user-defined functions. Please refer to http://www.w3.org/TR/xpath-functions/.

Attribute: as (optional): The type of the value returned when the function is evaluated. A sequence type holds the definition of the type of the data contained.

Attribute: override (optional): Specifies whether the function overrides any vendor-supplied function of the same name.

Sample Functions

Capitalize

This example creates a function that capitalizes the first letter of every word. It defines the function name, and a variable named chars that uses tokenize to split the sequence into substrings. It then uses a for-each to make the first letter of each substring a capital letter and the rest of the substring lower-case letters. Then it uses concat to reassemble the substrings, which have variable names, with spaces.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "http://commons.omniupdate.com/dtd/
standard.dtd">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/
XSL/Transform" xmlns:ou="http://omniupdate.com/XSL/Variables">
<!--  Main Template -->
<xsl:template match="/">
      <xsl:copy-of select="ou:capital('title case this sentence')"/>
</xsl:template>
 
<!-- Function Definition -->
<!--  CAPITALIZE THE FIRST LETTER OF EVERY WORD  -->
<xsl:function name="ou:capital">
<xsl:param name="string"/>
<xsl:variable name="chars" select="tokenize($string, ' ')"/>
      <xsl:for-each select="$chars">
            <xsl:variable name="key">
                  <xsl:value-of select="."/>
            </xsl:variable>
            <xsl:variable name="firstletter1">
                  <xsl:value-of select="upper-case(substring($key,1,1))"/>
            </xsl:variable>
            <xsl:variable name="rest1">
                  <xsl:value-of select="lower-case(substring($key,2))"/>
            </xsl:variable>
            <xsl:variable name="result">
                  <xsl:value-of select="concat($firstletter1,$rest1,' ')"/>
            </xsl:variable>
            <xsl:value-of select="$result"/>
      </xsl:for-each>
</xsl:function>
</xsl:stylesheet>

Clean content by replacing the delimiter with space.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "http://commons.omniupdate.com/dtd/
standard.dtd">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/
XSL/Transform" xmlns:ou="http://omniupdate.com/XSL/Variables">
<xsl:template match="/">
      <xsl:copy-of select="ou:clean('sample_text_with_underscore')"/>
</xsl:template>
<xsl:function name="ou:clean">
<xsl:param name="string"/>
<xsl:param name="delimiter"/>
<xsl:variable name="chars" select="tokenize($string,$delimiter)"/>
<xsl:for-each select="$chars">
      <xsl:variable name="key"><xsl:value-of select="."/></xsl:variable>
      <xsl:variable name="firstletter1">
<xsl:value-of select="upper-case(substring($key,1,1))" /></xsl:variable>
      <xsl:variable name="rest1">
<xsl:value-of select="lower-case(substring($key,2))" /></xsl:variable>
      <xsl:variable name="result">
<xsl:value-of select="concat($firstletter1,$rest1,' ')" /> </xsl:variable>
      <xsl:value-of select="$result" />
 </xsl:for-each>
</xsl:function>
</xsl:stylesheet>

Was this page helpful?

Additional Comments:


(Include your email address if you would like a reply)