<?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:value-of separator=" ">
      <xsl:choose>
	<xsl:when test="contains($sentence, ' ')">
	  <xsl:sequence select="str:reverse(substring-after($sentence, ' '))"/>
	  <xsl:sequence select="substring-before($sentence, ' ')"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:sequence select="$sentence"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:value-of>
  </xsl:function>

  <xsl:function name="str:reverse-xpath" 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:function name="str:reverse-easy" as="xs:string">
    <xsl:param name="sentence" as="xs:string"/>
    <xsl:value-of
	select="reverse(tokenize($sentence,' '))"/>
  </xsl:function>

</xsl:stylesheet>
