title: XSLT User Defined Function
body: <p>I am new to XSLT 2.0.  I am intrigued by User Defined functions ( <code>&lt;xsl:function</code> ). In particular, I'd like to use a UDF to make the code more modular and readable.</p>
  
  <p>I have this xsl:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
  &lt;xsl:stylesheet
     version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="xml" indent="yes"/&gt;
  &lt;xsl:template match="/"&gt;
      &lt;xsl:variable name="stopwords"
          select="document('stopwords.xml')//w/string()"/&gt;
               &lt;wordcount&gt;
                  &lt;xsl:for-each-group group-by="." select="
                      for $w in //text()/tokenize(., '\W+')[not(.=$stopwords)] return $w"&gt;
                      &lt;xsl:sort select="count(current-group())" order="descending"/&gt;            
                      &lt;word word="{current-grouping-key()}" frequency="{count(current-group())}"/&gt;
                  &lt;/xsl:for-each-group&gt;
               &lt;/wordcount&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Can want to add more condition testing (for example, exclude digits) to the 
  <code>for $w in //text()/tokenize(., '\W+')[not(.=$stopwords)]</code> 
  but the code would get messy. </p>
  
  <p>Is a UDF an option to tidy up that section of code if I make it more complex. Is it good practice to do so?</p>