Skip to content

Instantly share code, notes, and snippets.

@andrewminton
Created March 14, 2012 12:54
Show Gist options
  • Save andrewminton/2036268 to your computer and use it in GitHub Desktop.
Save andrewminton/2036268 to your computer and use it in GitHub Desktop.
<data>
<h1>This is a H1 outside of content block</h1>
<page-content id="82">
<h2 class="this-is-another-class and-another">The Gorilla Team</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac metus non ante sodales vulputate. Cras porttitor vestibulum varius. Maecenas tristique tortor id nulla gravida venenatis. Integer dapibus, sapien vitae aliquam laoreet, libero massa elementum ante, eu pulvinar erat odio vel augue. Nulla facilisi. Praesent vestibulum bibendum metus.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac metus non ante sodales vulputate. Cras porttitor vestibulum varius. Maecenas tristique tortor id nulla gravida venenatis. Integer dapibus, sapien vitae aliquam laoreet, libero massa elementum ante, eu pulvinar erat odio vel augue. Nulla facilisi. Praesent vestibulum bibendum metus.</p>
<h2>This is another h2 I don't want affected</h2>
</page-content>
</data>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="data">
<xsl:apply-templates select="page-content/*" mode="html"/>
</xsl:template>
<xsl:template match="*" mode="html">
<xsl:element name="{name()}">
<xsl:apply-templates select="* | @* | text()" mode="html"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*" mode="html">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="h2" mode="html">
<h2> <!--if the H2 follows the H1 add a class -->
<xsl:choose>
<xsl:when test="name(following-sibling::*[1])">
<xsl:attribute name="class">
<xsl:value-of select="@class"/>
<xsl:text> sub-title</xsl:text>
</xsl:attribute>
<xsl:apply-templates select="* | text()" mode="html"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="* | @* | text()" mode="html"/>
</xsl:otherwise>
</xsl:choose>
</h2>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment