[ACCEPTED]-XSLT output formatting: removing line breaks, and blank output lines from removed elements while keeping indent-xslt

Accepted answer
Score: 16

Adding these templates to your stylesheet:

<xsl:template match="*/text()[normalize-space()]">
    <xsl:value-of select="normalize-space()"/>
</xsl:template>

<xsl:template match="*/text()[not(normalize-space())]" />

Produces 1 this output:

<?xml version="1.0" encoding="UTF-8"?>
<newdoc xmlns="http://www/w3.org/1999/xhtml">
    <segment xmlns="" xmlns:foo="http://www.foo.org" title="Mr. Title">
        <h2>Mr. Title</h2>
        <p>This is one paragraph.</p>
        <p>Another paragraph.</p>
        <ol>
            <li>An item paragraph.</li>
            <li>Another item paragraph</li>
        </ol>
    </segment>
</newdoc>
Score: 8

At the very end of the stylesheet add these two templates:

<xsl:template match=
"text()[not(string-length(normalize-space()))]"/>

<xsl:template match=
"text()[string-length(normalize-space()) > 0]">
  <xsl:value-of select="translate(.,'&#xA;&#xD;', '  ')"/>
</xsl:template>

You now get the wanted result:

<?xml version="1.0" encoding="UTF-8"?>
<newdoc xmlns="http://www/w3.org/1999/xhtml">
   <segment xmlns="" xmlns:foo="http://www.foo.org" title="Mr. Title">
      <h2>Mr. Title</h2>
      <p>This is one paragraph.         </p>
      <p>Another paragraph.         </p>
      <ol>
         <li>An item paragraph.</li>
         <li>Another item paragraph</li>
      </ol>
   </segment>
</newdoc>

0

More Related questions