title: Parsing plain text in CDATA to html with XSLT 2.0/3.0 using multiple steps. Part way there
body: <p>I have a working process using XSLT 2.0/3.0 using Saxon-HE latest version that supports XSLT 3.0 that takes the text of a CDATA section in an XML file does markup into HTML. The text has no HTML tags but minimal plain text markup that relies on new lines and markup in square brackets at the beginning of lines. The working process (not shown here) uses multiple stages of setting the text in a variable, using replace functions with patterns and tons of <code>&amp;lt;</code>, <code>&amp;gt;</code> <code>&amp;apos;</code> etc to gradually getting to the final step. Not only is it difficult to read, but it's not very extensible if I want to add another change to the markup. I've started trying to build a better markup process below but getting stuck. </p>
  
  <p>Here is a small sample of my trimmed XML file structure:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project&gt;
  ---------------------------------------------------
  &lt;document&gt;
  &lt;docText&gt;&lt;![CDATA[
  [page 001] 1
  [margin] Person1 to Person2
  This Indenture made this x''th Day of y in the year z Between person1,     grantor, of place1 to person2, grantee, of place2 for 5 dollars ... the s''d person1 to s''d person2 ... signed under my hand.
  
  Witnesses present
  [signed] Mrs. Jane Doe (seal)
  [witness] Mr. Witness1
  [witness] Ms. Witness1
  
  Court office month x''th year
  I do hereby certify that ... and is thereon truly admitted to Record
  [clerk] John G. Reynolds DCCC
  ]]&gt;&lt;/docText&gt;
  &lt;persons&gt;
  &lt;person role="grantor"&gt;Jane Doe&lt;/person&gt;
  &lt;person role="grantee"&gt;Bob Jones&lt;/person&gt;
  &lt;/persons&gt;
  &lt;/document&gt;
  ---------------------------------------------------
  &lt;document&gt;
  &lt;docText&gt;&lt;![CDATA[
  [page 002] 2
  [margin] Person3 to Person4
  This Indenture made this x''th Day of y in the year z Between person1, grantor, of place1 to person2, grantee, of place2 for 5 dollars ... the s''d person1 to s''d person2 ... signed under my hand.
  
  Witnesses present
  [signed] Mr. John Doe (seal)
  [witness] Mr. Witness1
  [witness] Ms. Witness1
  
  [page 003] 3
  
  Court office month x''th year
  I do hereby certify that ... and is thereon truly admitted to Record
  [clerk] John G. Reynolds DCCC
  ]]&gt;&lt;/docText&gt;
  &lt;persons&gt;
  &lt;person role="grantor"&gt;John Doe&lt;/person&gt;
  &lt;person role="grantee"&gt;Bob Jones&lt;/person&gt;
  &lt;/persons&gt;
  &lt;/document&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>These are some of the steps I want to take with the text in CDATA</p>
  
  <ol>
  <li>tokenize all lines using \n new line</li>
  <li>lines which begin with a word in square brackets (e.g., [witness]) are tagged with <code>&lt;div&gt;</code> using class in brackets (e.g., <code>&lt;div class="witness"&gt;rest of line&lt;/div&gt;</code>)</li>
  <li>remaining lines are tagged with <code>&lt;p&gt;</code> tags</li>
  <li>all blank lines are eliminated</li>
  <li>scan text in the <code>&lt;div&gt;</code> and <code>&lt;p&gt;</code> text nodes above for further processing:</li>
  <li>find any pair of single quotes (i.e. paired apostrophe) followed by 1 to 4 upper or lower case letters and place in <code>&lt;sup&gt;&lt;/sup&gt;</code> (e.g., 25''th becomes <code>25&lt;sup&gt;th&lt;/sup&gt;</code>)</li>
  <li><p>group adjacent <code>&lt;div&gt;</code> of same class name into outer <code>&lt;div&gt;</code> of a certain name e.g.</p>
  
  <pre><code>&lt;div class="a"&gt;b&lt;/div&gt;
  &lt;div class="a"&gt;b&lt;/div&gt;
  becomes
  &lt;div class="a-outer"&gt;
  &lt;div class="a"&gt;b&lt;/div&gt;
  &lt;div class="a"&gt;b&lt;/div&gt;
  &lt;/div&gt;
  </code></pre></li>
  <li>additional markup as needed.</li>
  </ol>
  
  <p>I have what I want through step 6 (half of 5), though likely poor structure. This stylesheet works and gives me most of what I had in the much longer previous stylesheet and templates.</p>
  
  <p>Here is a shortened version of my <strong>XSLT 3.0</strong> stylesheet and templates:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="3.0" 
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:my="my:functions" 
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  exclude-result-prefixes="xsl xs my fn" expand-text="yes"&gt;
  
  &lt;xsl:output method="html" html-version="5.0" encoding="utf-8"  indent="yes"/&gt;
  &lt;xsl:template match="/"&gt;
  &lt;html&gt;
        &lt;head&gt;
        &lt;title&gt;Test Title&lt;/title&gt;
        &lt;style&gt;
      div {{background-color: pink;}}
      p {{background-color: ; clear: right; margin-bottom: 0;}}
      .clerk, .signed {{float:right;}}
      .margin::before {{content: "[margin note:] ";}}
      .clear {{clear: right;}}
        &lt;/style&gt;
        &lt;/head&gt;
        &lt;body&gt;
             &lt;h2&gt;Records&lt;/h2&gt;
             &lt;xsl:apply-templates select="project/document"/&gt;
        &lt;/body&gt;
   &lt;/html&gt;
   &lt;/xsl:template&gt;
  
   &lt;xsl:template match="document"&gt;
        &lt;article&gt;
        &lt;h3&gt;&lt;xsl:value-of select="persons/person[@role='grantor']"/&gt; to 
        &lt;xsl:value-of select="persons/person[@role='grantee']"/&gt;&lt;/h3&gt;
        &lt;xsl:apply-templates select="docText"/&gt; &lt;!-- docText contains text inside CDATA section --&gt;
        &lt;div class="clear"/&gt;
        &lt;/article&gt;&lt;hr /&gt;
   &lt;/xsl:template&gt;
  
   &lt;!-- all lines of text are parsed here and tagged with either &lt;p&gt; or  &lt;div&gt; and blank lines discarded--&gt;
  &lt;xsl:template match="docText"&gt;
  &lt;xsl:variable name="vLines" select="fn:analyze-string(., '\n')" /&gt;
  &lt;xsl:for-each select="$vLines/fn:non-match"&gt;
  &lt;xsl:choose&gt;
  &lt;xsl:when test="starts-with(.,'[')"&gt;
      &lt;xsl:variable name="v2" select="fn:analyze-string(.,'\[(witness|signed|clerk|margin)\]')"/&gt;
      &lt;div class="{fn:replace($v2/fn:match , '\[(.*?)\]' , '$1')}"&gt;{$v2/fn:non-match}&lt;/div&gt;
  &lt;/xsl:when&gt;
  &lt;xsl:otherwise&gt;
      &lt;p&gt;
      &lt;xsl:call-template name="tReplaceDblApos"&gt;
      &lt;xsl:with-param name="pText" select="."/&gt;
      &lt;/xsl:call-template&gt;
      &lt;/p&gt;
  &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;
  &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
  
   &lt;!-- any 1 to 4 characters following two adjacent single quotes is tagged with &lt;sup&gt; without quotes--&gt;
   &lt;xsl:template name="tReplaceDblApos"&gt;
   &lt;xsl:param name="pText"/&gt;
   &lt;xsl:analyze-string select="$pText" regex="''([a-zA-Z]{{1,4}})"&gt;
   &lt;xsl:matching-substring&gt;
        &lt;sup&gt;&lt;xsl:value-of select="regex-group(1)"/&gt;&lt;/sup&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:template&gt;
  
   &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>I would appreciate any suggestion for better ways of accomplishing this type of markup and how to make it extensible and accomplish the last step listed for example. I've tried off and on the last several months to make the process simpler, and this is the closest I've gotten so far.
  Apologies for any misuse of terminology, the long example, and the novice state of the code.</p>
  
  <p>Michael</p>