Skip to content

Instantly share code, notes, and snippets.

@quirijnslings
Created August 31, 2020 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quirijnslings/1819aa66471e57816c8e14a186424b0d to your computer and use it in GitHub Desktop.
Save quirijnslings/1819aa66471e57816c8e14a186424b0d to your computer and use it in GitHub Desktop.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"></xsl:output>
<xsl:template match="/ | node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="*[ (self::br or self::p or self::div) and normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos; and not(@*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or @* or * or node()]) and not(following::node()[not( (self::text() or self::br or self::p or self::div) and normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos; and not(@*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or @* or * or node()]) )]) ]">
<!-- ignore all paragraphs and line-breaks at the end that have nothing but (non-breaking) spaces and line breaks -->
</xsl:template>
<xsl:template match="br[parent::div and not(preceding-sibling::node()) and not(following-sibling::node())]">
<!-- Chrome generates <div><br/></div>. Renders differently in different browsers. Replace it with a non-breaking space -->
<xsl:text></xsl:text>
</xsl:template>
<xsl:template match="@class[starts-with(string(.),&apos;Mso&apos;)]"/>
<xsl:template match="@class[starts-with(string(.),&apos;xl&apos;)]"/>
<xsl:template match="style|@style|comment()|@xml:lang|@lang"/>
<xsl:template match="@cellspacing|@cellpadding|colgroup"/>
<xsl:template match="@width[parent::table]|@border[parent::table]"/>
<xsl:template match="@width[parent::td]|@height[parent::td]"/>
<xsl:template match="span">
<xsl:apply-templates select="node() | @*"/>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment