Skip to content

Instantly share code, notes, and snippets.

@0mg
Created September 17, 2010 19:59
Show Gist options
  • Select an option

  • Save 0mg/584832 to your computer and use it in GitHub Desktop.

Select an option

Save 0mg/584832 to your computer and use it in GitHub Desktop.
XMLStyle.XSLT
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<root>
<style xmlns="http://www.w3.org/1999/xhtml">
root {
font-family: monospace;
}
element, char, comment, proc {
display: block;
margin-left: 2ex;
}
tag name, attr name, proc name {
color: blue;
}
attr value, proc value {
color: green;
}
char {
color: red;
}
comment value {
color: silver;
}
</style>
<xsl:call-template name="main"/>
</root>
</xsl:template>
<xsl:template name="main">
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test=".=self::processing-instruction()">
<proc><tag>&lt;?<name><xsl:value-of select="name()"/></name><xsl:value-of select="' '"/><value><xsl:value-of select="."/></value>?&gt;</tag></proc>
</xsl:when>
<xsl:when test=".=self::*">
<element>
<xsl:choose>
<xsl:when test="node()">
<tag>&lt;<name><xsl:value-of select="name()"/></name><xsl:call-template name="attr"/>&gt;</tag>
<xsl:call-template name="main"/>
<tag>&lt;/<name><xsl:value-of select="name()"/></name>&gt;</tag>
</xsl:when>
<xsl:when test="not(node())">
<tag>&lt;<name><xsl:value-of select="name()"/></name><xsl:call-template name="attr"/>/&gt;</tag>
</xsl:when>
</xsl:choose>
</element>
</xsl:when>
<xsl:when test=".=self::text()">
<char><xsl:value-of select="."/></char>
</xsl:when>
<xsl:when test=".=self::comment()">
<comment><tag>&lt;!--</tag><value><xsl:value-of select="."/></value><tag>--&gt;</tag></comment>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template name="attr">
<xsl:for-each select="@*">
<xsl:value-of select="' '"/><attr><name><xsl:value-of select="name()"/></name>="<value><xsl:value-of select="."/></value>"</attr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment