title: Use xslt:analyze-string to add acronyms to HTML - now with synonyms
body: <p>I already posted the question how to add acronym tags to HTML text and got a nice solution (see <a href="https://stackoverflow.com/questions/4445567/use-xsltanalyze-string-to-add-acronyms-to-html">Use xslt:analyze-string to add acronyms to HTML</a> ). Thank you!</p>
  
  <p>Now I added synonyms to my acronyms and adapted the solution - it works fine.</p>
  
  <p>My only question: is it useful to put the xsl:analyze-string instruction for the synonyms inside the xsl:non-matching-substring part of the first xsl:analyze-string of the main word (name)?
  Are there other ways to implement this?</p>
  
  <p>Below my source and transformation.</p>
  
  <p>Thanks for your hints! :-)</p>
  
  <p>Suidu</p>
  
  <p>source.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;doc&gt;
      &lt;dictionary&gt;
  
          &lt;acronym name="WWW"&gt;
              &lt;synonym&gt;www&lt;/synonym&gt;
              &lt;description&gt;The World Wide Web&lt;/description&gt;
          &lt;/acronym&gt;
  
          &lt;acronym name="HTML"&gt;
              &lt;synonym&gt;html&lt;/synonym&gt;
              &lt;description&gt;The HyperText Markup Language&lt;/description&gt;
          &lt;/acronym&gt;
  
      &lt;/dictionary&gt;
  
      &lt;div&gt;
          &lt;p&gt;In the &lt;strong&gt;www&lt;/strong&gt; you can find a lot of &lt;em&gt;html&lt;/em&gt; documents.&lt;/p&gt; 
      &lt;/div&gt;
  
  &lt;/doc&gt; 
  </code></pre>
  
  <p>transformation.xsl:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:my="my:my"  exclude-result-prefixes="my"&gt;
  &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt;
  &lt;xsl:strip-space elements="*"/&gt;
  
  &lt;xsl:template match="node()|@*"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="node()|@*"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/*"&gt;
      &lt;xsl:apply-templates/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="text()" priority="0.1"&gt;
      &lt;xsl:sequence select="my:insert-acronyms(., /*/dictionary/acronym)"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:function name="my:insert-acronyms" as="node()*"&gt;
      &lt;xsl:param name="text" as="text()"/&gt;
      &lt;xsl:param name="acronyms" as="node()*"/&gt;
  
      &lt;xsl:sequence select=
           "if($acronyms)
              then my:replace-words($text, $acronyms/@name, $acronyms/synonym)
              else $text
           "/&gt;
  &lt;/xsl:function&gt;
  
  &lt;xsl:function name="my:replace-words" as="node()*"&gt;
      &lt;xsl:param name="text" as="text()" /&gt;
      &lt;xsl:param name="names" as="node()*" /&gt;
      &lt;xsl:param name="synonyms" as="node()*" /&gt;
  
      &lt;xsl:analyze-string select="$text" 
           regex="{concat('(^|\W)(', string-join($names, '|'), ')(\W|$)')}"&gt;
           &lt;xsl:matching-substring&gt;
            &lt;xsl:value-of select="regex-group(1)"/&gt;
            &lt;acronym title="{$names[. eq regex-group(2)]/../description}"&gt;
             &lt;xsl:value-of select="regex-group(2)"/&gt;
            &lt;/acronym&gt;
            &lt;xsl:value-of select="regex-group(3)"/&gt;
           &lt;/xsl:matching-substring&gt;
           &lt;xsl:non-matching-substring&gt;
  
              &lt;xsl:analyze-string select="." 
                   regex="{concat('(^|\W)(', string-join($synonyms, '|'), ')(\W|$)')}"&gt;
                   &lt;xsl:matching-substring&gt;
                    &lt;xsl:value-of select="regex-group(1)"/&gt;
                    &lt;acronym title="{$synonyms[. eq regex-group(2)]/../description}"&gt;
                     &lt;xsl:value-of select="regex-group(2)"/&gt;
                    &lt;/acronym&gt;
                    &lt;xsl:value-of select="regex-group(3)"/&gt;
                   &lt;/xsl:matching-substring&gt;
                   &lt;xsl:non-matching-substring&gt;
                      &lt;xsl:value-of select="."/&gt;
                   &lt;/xsl:non-matching-substring&gt;
              &lt;/xsl:analyze-string&gt;
  
  
           &lt;/xsl:non-matching-substring&gt;
      &lt;/xsl:analyze-string&gt;
  &lt;/xsl:function&gt;
  
  &lt;xsl:template match="dictionary"/&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>