<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns="http://www.w3.org/1999/xhtml"
		xmlns:str="http://example.org/xslt/functions"
		xmlns:xs="http://www.w3.org/2001/XMLSchema"
		exclude-result-prefixes="xs str"
		version="2.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:text>str:reverse('DOG BITES MAN') = </xsl:text>
    <xsl:value-of select="str:reverse('DOG BITES MAN')"/>
  </xsl:template>

  <xsl:function name="str:reverse" as="xs:string">
    <xsl:param name="sentence" as="xs:string"/>
    <xsl:sequence select="if (contains($sentence, ' '))
                          then concat(str:reverse(substring-after($sentence, ' ')),
                                      ' ',
                                      substring-before($sentence, ' '))
                          else $sentence"/>
  </xsl:function>

</xsl:stylesheet>

