<div dir="auto">Clueless on this stylesheet.  Beyond me!  The Java below makes more sense to me!  Now my thought is to replace CDATA Sections inside comments. Argh!</div><div dir="auto"><br></div><div dir="auto"><div style="font-size:inherit"><xsl:stylesheet version="2.0" xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</a>"><br style="font-size:inherit">  <br style="font-size:inherit">  <!-- Identity template: copy everything as-is --><br style="font-size:inherit">  <xsl:template match="@*|node()"><br style="font-size:inherit">    <xsl:copy><br style="font-size:inherit">      <xsl:apply-templates select="@*|node()"/><br style="font-size:inherit">    </xsl:copy><br style="font-size:inherit">  </xsl:template><br style="font-size:inherit">  <br style="font-size:inherit">  <!-- Handle comments --><br style="font-size:inherit">  <xsl:template match="comment()"><br style="font-size:inherit">    <xsl:variable name="normalized"><br style="font-size:inherit">      <xsl:call-template name="normalize-tag-newlines"><br style="font-size:inherit">        <xsl:with-param name="text" select="."/><br style="font-size:inherit">      </xsl:call-template><br style="font-size:inherit">    </xsl:variable><br style="font-size:inherit">    <br style="font-size:inherit">    <!-- Split by remaining newlines --><br style="font-size:inherit">    <xsl:for-each select="tokenize($normalized, '\n')"><br style="font-size:inherit">      <xsl:if test="normalize-space(.) != ''"><br style="font-size:inherit">        <xsl:comment><br style="font-size:inherit">          <xsl:value-of select="normalize-space(.)"/><br style="font-size:inherit">        </xsl:comment><br style="font-size:inherit">      </xsl:if><br style="font-size:inherit">    </xsl:for-each><br style="font-size:inherit">  </xsl:template><br style="font-size:inherit">  <br style="font-size:inherit">  <!-- Normalize newlines within tags --><br style="font-size:inherit">  <xsl:template name="normalize-tag-newlines"><br style="font-size:inherit">    <xsl:param name="text"/><br style="font-size:inherit">    <br style="font-size:inherit">    <!-- First normalize start tags (including self-closing tags) --><br style="font-size:inherit">    <xsl:variable name="step1"><br style="font-size:inherit">      <xsl:analyze-string select="$text" regex="&lt;([^/&gt;][^&gt;]*)&gt;" flags="s"><br style="font-size:inherit">        <xsl:matching-substring><br style="font-size:inherit">          <xsl:text>&lt;</xsl:text><br style="font-size:inherit">          <xsl:value-of select="replace(regex-group(1), '\s*\n\s*', ' ')"/><br style="font-size:inherit">          <xsl:text>&gt;</xsl:text><br style="font-size:inherit">        </xsl:matching-substring><br style="font-size:inherit">        <xsl:non-matching-substring><br style="font-size:inherit">          <xsl:value-of select="."/><br style="font-size:inherit">        </xsl:non-matching-substring><br style="font-size:inherit">      </xsl:analyze-string><br style="font-size:inherit">    </xsl:variable><br style="font-size:inherit">    <br style="font-size:inherit">    <!-- Then normalize end tags --><br style="font-size:inherit">    <xsl:analyze-string select="$step1" regex="&lt;(/[^&gt;]*)&gt;"><br style="font-size:inherit">      <xsl:matching-substring><br style="font-size:inherit">        <xsl:text>&lt;</xsl:text><br style="font-size:inherit">        <xsl:value-of select="replace(regex-group(1), '\s*\n\s*', ' ')"/><br style="font-size:inherit">        <xsl:text>&gt;</xsl:text><br style="font-size:inherit">      </xsl:matching-substring><br style="font-size:inherit">      <xsl:non-matching-substring><br style="font-size:inherit">        <xsl:value-of select="."/><br style="font-size:inherit">      </xsl:non-matching-substring><br style="font-size:inherit">    </xsl:analyze-string><br style="font-size:inherit">  </xsl:template><br style="font-size:inherit">  <br style="font-size:inherit"></xsl:stylesheet></div><br></div><div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, Jan 5, 2026 at 2:09 PM John Carlson <<a href="mailto:yottzumm@gmail.com">yottzumm@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Okay, heres one that replaces newlines with spaces between < and > inside comments and add comments for newlines in comments elsewhere.  Stylesheet is next!<div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div><p></p></div>    <div>
    <div>
      
      
    </div>
    
    </div></blockquote></div></div>
</blockquote></div></div>
</div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div>
</div>
</blockquote></div></div>
</div>
</blockquote></div></div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div></div>
</blockquote></div></div>
</blockquote></div></div>
</blockquote></div></div>
</blockquote></div></div><div dir="auto"><br></div><div dir="auto"><div style="font-size:inherit">import org.xml.sax.SAXException;<br style="font-size:inherit">import org.xml.sax.ext.DefaultHandler2;<br style="font-size:inherit">import java.io.IOException;<br style="font-size:inherit">import java.io.Writer;<br style="font-size:inherit">import java.util.regex.Pattern;<br style="font-size:inherit">import java.util.regex.Matcher;<br style="font-size:inherit"><br style="font-size:inherit">public class CommentNormalizingHandler extends DefaultHandler2 {<br style="font-size:inherit">    private Writer writer;<br style="font-size:inherit">    <br style="font-size:inherit">    public CommentNormalizingHandler(Writer writer) {<br style="font-size:inherit">        this.writer = writer;<br style="font-size:inherit">    }<br style="font-size:inherit">    <br style="font-size:inherit">    @Override<br style="font-size:inherit">    public void comment(char[] ch, int start, int length) throws SAXException {<br style="font-size:inherit">        try {<br style="font-size:inherit">            String commentText = new String(ch, start, length);<br style="font-size:inherit">            <br style="font-size:inherit">            // Check if comment contains XML tag-like structures with newlines<br style="font-size:inherit">            if (containsTagWithNewlines(commentText)) {<br style="font-size:inherit">                // Normalize newlines within tags but keep multi-line structure outside tags<br style="font-size:inherit">                String normalized = normalizeTagNewlines(commentText);<br style="font-size:inherit">                <br style="font-size:inherit">                // Now split by remaining newlines (those outside tags)<br style="font-size:inherit">                String[] lines = normalized.split("\\n");<br style="font-size:inherit">                for (String line : lines) {<br style="font-size:inherit">                    String trimmed = line.trim();<br style="font-size:inherit">                    if (!trimmed.isEmpty()) {<br style="font-size:inherit">                        writer.write("<!--");<br style="font-size:inherit">                        writer.write(trimmed);<br style="font-size:inherit">                        writer.write("-->");<br style="font-size:inherit">                    }<br style="font-size:inherit">                }<br style="font-size:inherit">            } else {<br style="font-size:inherit">                // No tags with newlines - safe to split normally<br style="font-size:inherit">                String[] lines = commentText.split("\\n");<br style="font-size:inherit">                for (String line : lines) {<br style="font-size:inherit">                    String trimmed = line.trim();<br style="font-size:inherit">                    if (!trimmed.isEmpty()) {<br style="font-size:inherit">                        writer.write("<!--");<br style="font-size:inherit">                        writer.write(trimmed);<br style="font-size:inherit">                        writer.write("-->");<br style="font-size:inherit">                    }<br style="font-size:inherit">                }<br style="font-size:inherit">            }<br style="font-size:inherit">        } catch (IOException e) {<br style="font-size:inherit">            throw new SAXException("Error writing comment", e);<br style="font-size:inherit">        }<br style="font-size:inherit">    }<br style="font-size:inherit">    <br style="font-size:inherit">    private boolean containsTagWithNewlines(String text) {<br style="font-size:inherit">        // Check for start tags with newlines: <tag...newline...><br style="font-size:inherit">        Pattern startTagPattern = Pattern.compile("<[^/>][^>]*\\n[^>]*>", Pattern.DOTALL);<br style="font-size:inherit">        if (startTagPattern.matcher(text).find()) {<br style="font-size:inherit">            return true;<br style="font-size:inherit">        }<br style="font-size:inherit">        <br style="font-size:inherit">        // Check for end tags with newlines: </tag...newline...><br style="font-size:inherit">        Pattern endTagPattern = Pattern.compile("</[^>]*\\n[^>]*>");<br style="font-size:inherit">        if (endTagPattern.matcher(text).find()) {<br style="font-size:inherit">            return true;<br style="font-size:inherit">        }<br style="font-size:inherit">        <br style="font-size:inherit">        return false;<br style="font-size:inherit">    }<br style="font-size:inherit">    <br style="font-size:inherit">    private String normalizeTagNewlines(String text) {<br style="font-size:inherit">        // Replace newlines within start tags (between < and >)<br style="font-size:inherit">        Pattern startTagPattern = Pattern.compile("<([^/>][^>]*)>", Pattern.DOTALL);<br style="font-size:inherit">        Matcher startMatcher = startTagPattern.matcher(text);<br style="font-size:inherit">        StringBuffer sb = new StringBuffer();<br style="font-size:inherit">        <br style="font-size:inherit">        while (startMatcher.find()) {<br style="font-size:inherit">            String tagContent = startMatcher.group(1);<br style="font-size:inherit">            String normalized = tagContent.replaceAll("\\s*\\n\\s*", " ");<br style="font-size:inherit">            startMatcher.appendReplacement(sb, "<" + Matcher.quoteReplacement(normalized) + ">");<br style="font-size:inherit">        }<br style="font-size:inherit">        startMatcher.appendTail(sb);<br style="font-size:inherit">        <br style="font-size:inherit">        // Replace newlines within end tags<br style="font-size:inherit">        Pattern endTagPattern = Pattern.compile("<(/[^>]*)>");<br style="font-size:inherit">        Matcher endMatcher = endTagPattern.matcher(sb.toString());<br style="font-size:inherit">        StringBuffer sb2 = new StringBuffer();<br style="font-size:inherit">        <br style="font-size:inherit">        while (endMatcher.find()) {<br style="font-size:inherit">            String tagContent = endMatcher.group(1);<br style="font-size:inherit">            String normalized = tagContent.replaceAll("\\s*\\n\\s*", " ");<br style="font-size:inherit">            endMatcher.appendReplacement(sb2, "<" + Matcher.quoteReplacement(normalized) + ">");<br style="font-size:inherit">        }<br style="font-size:inherit">        endMatcher.appendTail(sb2);<br style="font-size:inherit">        <br style="font-size:inherit">        return sb2.toString();</div></div><div dir="auto"><div style="font-size:inherit"><br style="font-size:inherit">    }<br style="font-size:inherit">    <br style="font-size:inherit">    @Override<br style="font-size:inherit">    public void startElement(String uri, String localName, String qName, <br style="font-size:inherit">                            org.xml.sax.Attributes attributes) throws SAXException {<br style="font-size:inherit">        try {<br style="font-size:inherit">            writer.write("<");<br style="font-size:inherit">            writer.write(qName);<br style="font-size:inherit">            <br style="font-size:inherit">            for (int i = 0; i < attributes.getLength(); i++) {<br style="font-size:inherit">                writer.write(" ");<br style="font-size:inherit">                writer.write(attributes.getQName(i));<br style="font-size:inherit">                writer.write("='");<br style="font-size:inherit">                writer.write(escapeXml(attributes.getValue(i)));<br style="font-size:inherit">                writer.write("'");<br style="font-size:inherit">            }<br style="font-size:inherit">            <br style="font-size:inherit">            writer.write(">");<br style="font-size:inherit">        } catch (IOException e) {<br style="font-size:inherit">            throw new SAXException("Error writing start element", e);<br style="font-size:inherit">        }<br style="font-size:inherit">    }<br style="font-size:inherit">    <br style="font-size:inherit">    @Override<br style="font-size:inherit">    public void endElement(String uri, String localName, String qName) throws SAXException {<br style="font-size:inherit">        try {<br style="font-size:inherit">            writer.write("</");<br style="font-size:inherit">            writer.write(qName);<br style="font-size:inherit">            writer.write(">");<br style="font-size:inherit">        } catch (IOException e) {<br style="font-size:inherit">            throw new SAXException("Error writing end element", e);<br style="font-size:inherit">        }<br style="font-size:inherit">    }<br style="font-size:inherit">    <br style="font-size:inherit">    @Override<br style="font-size:inherit">    public void characters(char[] ch, int start, int length) throws SAXException {<br style="font-size:inherit">        try {<br style="font-size:inherit">            writer.write(escapeXml(new String(ch, start, length)));<br style="font-size:inherit">        } catch (IOException e) {<br style="font-size:inherit">            throw new SAXException("Error writing characters", e);<br style="font-size:inherit">        }<br style="font-size:inherit">    }<br style="font-size:inherit">    <br style="font-size:inherit">    @Override<br style="font-size:inherit">    public void startDocument() throws SAXException {<br style="font-size:inherit">        try {<br style="font-size:inherit">            writer.write("<?xml version='1.0' encoding='UTF-8'?>");<br style="font-size:inherit">        } catch (IOException e) {<br style="font-size:inherit">            throw new SAXException("Error writing start document", e);<br style="font-size:inherit">        }<br style="font-size:inherit">    }<br style="font-size:inherit">    <br style="font-size:inherit">    @Override<br style="font-size:inherit">    public void processingInstruction(String target, String data) throws SAXException {<br style="font-size:inherit">        try {<br style="font-size:inherit">            writer.write("<?");<br style="font-size:inherit">            writer.write(target);<br style="font-size:inherit">            if (data != null && !data.isEmpty()) {<br style="font-size:inherit">                writer.write(" ");<br style="font-size:inherit">                writer.write(data);<br style="font-size:inherit">            }<br style="font-size:inherit">            writer.write("?>");<br style="font-size:inherit">        } catch (IOException e) {<br style="font-size:inherit">            throw new SAXException("Error writing processing instruction", e);<br style="font-size:inherit">        }<br style="font-size:inherit">    }<br style="font-size:inherit">    <br style="font-size:inherit">    private String escapeXml(String text) {<br style="font-size:inherit">        return text.replace("&", "&amp;")<br style="font-size:inherit">                   .replace("<", "&lt;")<br style="font-size:inherit">                   .replace(">", "&gt;")<br style="font-size:inherit">                   .replace("'", "&apos;")<br style="font-size:inherit">                   .replace("\"", "&quot;");<br style="font-size:inherit">    }<br style="font-size:inherit">}</div><br></div>
</blockquote></div></div>