Skip to content

Instantly share code, notes, and snippets.

@joshhebb joshhebb/Cleanup-RTF.xslt
Last active Jul 16, 2019

Embed
What would you like to do?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xhtml">
<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>
<!-- remove disallowed attributes -->
<xsl:template match="@align | @valign | @border | @cellpadding | @cellspacing | @style | @colspan | @rowspan | @class"></xsl:template>
<!-- added bold to strong conversion -->
<xsl:template match="xhtml:string | b">
<xsl:element name="strong">
<xsl:apply-templates select="node()"></xsl:apply-templates>
</xsl:element>
</xsl:template>
<!-- added italic to em converter -->
<xsl:template match="xhtml:i | i">
<xsl:element name="em">
<xsl:apply-templates select="node()"></xsl:apply-templates>
</xsl:element>
</xsl:template>
<!-- added italic to em converter -->
<xsl:template match="xhtml:span | span | xhtml:div | div">
<xsl:element name="p">
<xsl:apply-templates select="node()"></xsl:apply-templates>
</xsl:element>
</xsl:template>
<!-- Remove but keep contents -->
<xsl:template match="ol | li">
<xsl:apply-templates/>
</xsl:template>
<!-- Remove -->
<xsl:template match="ol | li">
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.