Skip to content

Instantly share code, notes, and snippets.

@spodzone
Last active August 29, 2015 14:27
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 spodzone/675e40505b70c34fe5ee to your computer and use it in GitHub Desktop.
Save spodzone/675e40505b70c34fe5ee to your computer and use it in GitHub Desktop.
Pretty XSLT formatting
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:addthis="http://www.addthis.com/help/api-spec"
xmlns:fn2="http://www.w3.org/2004/07/xpath-functions"
xmlns:wv="http://www.openlinksw.com/Virtuoso/WikiV/"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl wv fn2 xhtml"
version="1.0">
<xsl:output
cdata-section-elements="style"
doctype-public="-//W3C//DTD XHTML+RDFa 1.0//EN"
doctype-system="http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"
indent="yes"
method="xml" />
<!-- Misc variables that might come in handy later -->
<xsl:variable name="this">
<xsl:value-of select="/xml/query/url" />
</xsl:variable>
<xsl:variable name="thisdata">
<xsl:copy-of select="/xml/bag[@id='thisdata']" />
</xsl:variable>
<!-- Incoming parameteres -->
<xsl:param name="site" />
<xsl:param name="url" />
<xsl:param name="rdfdata" />
<!-- Primary template -->
<xsl:template match="/">
<html
xmlns:bibo="http://purl.org/ontology/bibo/"
xmlns:cc="http://creativecommons.org/licenses/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dct="http://purl.org/dc/terms/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:exif="http://www.kanzaki.com/ns/exif"
xmlns:fb="http://www.facebook.com/2008/fbml"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:og="http://ogp.me/ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:rss="http://purl.org/rss/1.0"
xmlns:sioc="http://rdfs.org/sioc/ns#"
xmlns:skos="http://www.w3.org/2004/02/skos/core#"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns="http://www.w3.org/1999/xhtml"
itemid="{$url}"
itemscope="itemscope"
version="XHTML+RDFa 1.0">
<head>
<meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport" />
<meta content="text/html;charset=UTF-8" http-equiv="Content-type" />
<xsl:apply-templates mode="metahead" select="/xml/bag[@id='thisdata']/data" />
<title>
<xsl:value-of select="$site" />
:
<xsl:value-of select="//html/head/title" />
</title>
<xsl:comment>Embed RDF data</xsl:comment>
<script type="text/turtle">
<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
<xsl:value-of select="$rdfdata" />
<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
</script>
<script src="//example.org/somescript.js">
<xsl:text>
</xsl:text>
</script>
</head>
<body>
<!-- masthead -->
<header id="masthead">
<h1>Site name</h1>
<nav>
<ul>
<li>
<a href="https://twitter.com/">Twitter</a>
</li>
<li>
<a href="http://www.minds.com/">Minds</a>
</li>
<li>
<a href="http://www.facebook.com/">Facebook</a>
</li>
</ul>
</nav>
</header>
<section id="content">
<h1>
<xsl:value-of select="/html/head/title" />
</h1>
<xsl:apply-templates mode="copy" select="/html/body" />
</section>
</body>
</html>
</xsl:template>
<!-- Copy-mode templates -->
<xsl:template match="body" mode="copy">
<xsl:apply-templates />
</xsl:template>
<!-- Pattern exclusions -->
<!-- Remove specific elements from the body altogether -->
<xsl:template match="meta|link" mode="copy" />
<!-- Intercept and replace left navbar -->
<xsl:template match="nav[@id='leftnav']">
<nav id="leftnav">
<ul class="min">
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/about">About</a>
</li>
<li>
<a href="/contact">Contact</a>
</li>
</ul>
</nav>
</xsl:template>
<!-- Generic loop -->
<xsl:template match="*" mode="copy">
<xsl:element name="{name(.)}">
<xsl:apply-templates select="@*" />
<xsl:apply-templates mode="copy" />
</xsl:element>
</xsl:template>
<xsl:template match="@*" mode="copy">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment