Skip to content

Instantly share code, notes, and snippets.

@mccoder
Created March 1, 2010 17:57
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 mccoder/318606 to your computer and use it in GitHub Desktop.
Save mccoder/318606 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 method="html" encoding="UTF-8" />
<xsl:param name="indent-increment" select="' '" />
<xsl:template name="newline">
<xsl:text disable-output-escaping="yes">
</xsl:text>
</xsl:template>
<xsl:template match="comment() | processing-instruction()">
<xsl:param name="indent" select="''" />
</xsl:template>
<xsl:template match="br">
<xsl:call-template name="newline" />
</xsl:template>
<xsl:template match="object|script">
</xsl:template>
<xsl:template match="img[@width='1' and @height='1']">
</xsl:template>
<xsl:template match="img">
<xsl:call-template name="newline" />
<xsl:value-of select="'Image:[ '" />
<xsl:value-of select="@src" />
<xsl:value-of select="' ]'" />
</xsl:template>
<xsl:template match="text()">
<xsl:param name="indent" select="''" />
<xsl:value-of select="$indent" />
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
<xsl:template match="text()[normalize-space(.)='']" />
<!-- TABLE -->
<xsl:template match="th">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:variable name="r">
<xsl:apply-templates select="*|text()" />
</xsl:variable>
<xsl:value-of select="'*'" />
<xsl:value-of select="normalize-space($r)" />
<xsl:value-of select="'* | '" />
<xsl:call-template name="newline" />
</xsl:template>
<xsl:template match="td">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:variable name="r">
<xsl:apply-templates select="*|text()" />
</xsl:variable>
<xsl:value-of select="normalize-space($r)" />
<xsl:value-of select="' | '" />
<xsl:call-template name="newline" />
</xsl:template>
<xsl:template match="tr">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:value-of select="' '" />
<xsl:apply-templates select="td" />
</xsl:template>
<xsl:template match="tbody|thead|tfoot">
<xsl:param name="indent" select="''" />
<xsl:apply-templates select="*|text()" />
</xsl:template>
<xsl:template match="table">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:apply-templates select="thead" />
<xsl:apply-templates select="tbody" />
<xsl:apply-templates select="tr" />
<xsl:apply-templates select="tfoot" />
<xsl:call-template name="newline" />
</xsl:template>
<!-- END TABLE -->
<xsl:template match="pre">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:apply-templates select="*|text()" mode="pre" />
<xsl:call-template name="newline" />
</xsl:template>
<xsl:template match="h1|h2|h3|h4|h5">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:variable name="r">
<xsl:apply-templates select="*|text()" />
</xsl:variable>
<xsl:value-of select="normalize-space($r)" />
<xsl:call-template name="newline" />
</xsl:template>
<xsl:template match="a">
<xsl:param name="indent" select="''" />
<xsl:value-of select="' '" />
<xsl:choose>
<xsl:when
test="starts-with(@href,'http://') or starts-with(@href,'https://') or starts-with(@href,'ftp://') or starts-with(@href,'mailto:')">
<xsl:value-of select="'['" />
<xsl:variable name="r">
<xsl:apply-templates select="*|text()" />
</xsl:variable>
<xsl:value-of select="normalize-space($r)" />
<xsl:value-of select="'] '" />
<xsl:value-of select="@href" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="*|text()" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="li">
<xsl:param name="indent" select="''" />
<xsl:value-of select="$indent" />
<xsl:value-of select="'* '" />
<xsl:apply-templates select="*|text()">
<xsl:with-param name="indent"
select="concat ($indent, $indent-increment)" />
</xsl:apply-templates>
<xsl:call-template name="newline" />
</xsl:template>
<xsl:template match="ul|div|p">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:apply-templates select="*|text()">
<xsl:with-param name="indent"
select="concat ($indent, $indent-increment)" />
</xsl:apply-templates>
<xsl:call-template name="newline" />
</xsl:template>
<xsl:template match="b|i|u|strong|em">
<xsl:param name="indent" select="''" />
<xsl:value-of select="' '" />
<xsl:variable name="r">
<xsl:apply-templates select="*|text()" />
</xsl:variable>
<xsl:value-of select="normalize-space($r)" />
<xsl:value-of select="' '" />
</xsl:template>
<xsl:template match="center|body|html|span|blockquote">
<xsl:param name="indent" select="''" />
<xsl:value-of select="' '" />
<xsl:apply-templates select="*|text()" />
<xsl:value-of select="' '" />
</xsl:template>
<xsl:template match="*">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:value-of select="$indent" />
<xsl:choose>
<xsl:when test="count(child::*) > 0">
<xsl:apply-templates select="*|text()">
<xsl:with-param name="indent"
select="concat($indent, $indent-increment)" />
</xsl:apply-templates>
<xsl:call-template name="newline" />
<xsl:value-of select="$indent" />
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- MODE PRE -->
<xsl:template match="text()" mode="pre">
<xsl:param name="indent" select="''" />
<xsl:value-of select="$indent" />
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="ul|div|p" mode="pre">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:apply-templates select="*|text()" mode="pre">
<xsl:with-param name="indent"
select="concat ($indent, $indent-increment)" />
</xsl:apply-templates>
<xsl:call-template name="newline" />
</xsl:template>
<xsl:template match="b|i|u|center|body|strong|html|span|blockquote|em"
mode="pre">
<xsl:param name="indent" select="''" />
<xsl:value-of select="' '" />
<xsl:apply-templates select="*|text()" mode="pre" />
<xsl:value-of select="' '" />
</xsl:template>
<xsl:template match="*" mode="pre">
<xsl:param name="indent" select="''" />
<xsl:call-template name="newline" />
<xsl:value-of select="$indent" />
<xsl:choose>
<xsl:when test="count(child::*) > 0">
<xsl:apply-templates select="*|text()" mode="pre">
<xsl:with-param name="indent"
select="concat ($indent, $indent-increment)" />
</xsl:apply-templates>
<xsl:call-template name="newline" />
<xsl:value-of select="$indent" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates mode="pre" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment