Skip to content

Instantly share code, notes, and snippets.

@redblobgames
Created July 27, 2019 14:23
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 redblobgames/1e743e35e58a52be91879eeb37484f07 to your computer and use it in GitHub Desktop.
Save redblobgames/1e743e35e58a52be91879eeb37484f07 to your computer and use it in GitHub Desktop.
Example of how I use xsltproc to expand section headings
<!--
I write my site with xhtml extended with my x: tags, then process it into
regular html using xsltproc. Here's an example of how I use xslt for sections.
1. Expand x: tags to a standard html tag:
<x:section> … </x:section> becomes <section> … </section>
2. Pass attributes down to the html tag:
<x:section class="example"> … </x:section>
becomes <section class="example"> … </section>
3. Extract title attribute:
<x:section title="Hexagons"> … </x:section> becomes
<section><h2>Hexagons</h2> … </section> — this is an exception to rule 2,
in that not all attributes are passed down to the <section> tag
4. Extract id attribute to make section linkable:
<x:section title="Hexagons" id="hexagons"> … </x:section> becomes
<section><h2 id="hexagons">Hexagons <a href="#hexagons">#</a></h2> … </section>
-->
<xsl:template match="x:section">
<section>
<xsl:copy-of select="@*[local-name()!='id' and local-name()!='title']"/>
<xsl:if test="@title != ''">
<h2>
<xsl:copy-of select="@id"/>
<xsl:value-of select="@title"/>
<xsl:if test="@id != ''">
<a><xsl:attribute name="href"><xsl:value-of select="concat('#',@id)"/></xsl:attribute>#</a>
</xsl:if>
</h2>
</xsl:if>
<xsl:apply-templates/>
</section>
</xsl:template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment