Skip to content

Instantly share code, notes, and snippets.

@andybluntish
Created October 27, 2013 09:33
Show Gist options
  • Save andybluntish/7179640 to your computer and use it in GitHub Desktop.
Save andybluntish/7179640 to your computer and use it in GitHub Desktop.
Output multiple files from a single input using grunt-xsltproc.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error: 404</title>
</head>
<body>
<header role="banner"><h1>Error: 404</h1></header><main role="main"><p>Not Found</p></main>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error: 500</title>
</head>
<body>
<header role="banner"><h1>Error: 500</h1></header><main role="main"><p>Internal Server Error</p></main>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Errors</title>
</head>
<body>
<header role="banner"><h1>Errors</h1></header><main role="main"><ul>
<li><a href="error_404.html">404</a></li>
<li><a href="error_500.html">500</a></li>
</ul></main>
</body>
</html>
<?xml version="1.0"?>
<errors>
<error id="404">Not Found</error>
<error id="500">Internal Server Error</error>
</errors>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">
<xsl:output method="html" omit-xml-declaration="yes" encoding="utf-8" indent="yes" media-type="text/html" />
<xsl:template match="/">
<html>
<head>
<title>Errors</title>
</head>
<body>
<header role="banner">
<h1>Errors</h1>
</header>
<main role="main">
<ul>
<xsl:apply-templates />
</ul>
</main>
</body>
</html>
</xsl:template>
<xsl:template match="error">
<li><a href="error_{@id}.html"><xsl:value-of select="@id" /></a></li>
<exsl:document href="error_{@id}.html" method="html">
<html>
<head>
<title>Error: <xsl:value-of select="@id" /></title>
</head>
<body>
<header role="banner">
<h1>Error: <xsl:value-of select="@id" /></h1>
</header>
<main role="main">
<p><xsl:value-of select="." /></p>
</main>
</body>
</html>
</exsl:document>
</xsl:template>
</xsl:stylesheet>
module.exports = function(grunt) {
grunt.initConfig({
xsltproc: {
options: {
stylesheet: 'errors.xsl'
},
compile: {
files: {
'errors.html': ['errors.xml']
}
}
}
});
grunt.loadNpmTasks('grunt-xsltproc');
grunt.registerTask('default', ['xsltproc']);
};
{
"name": "errors",
"version": "0.0.0",
"dependencies": {},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-xsltproc": "~0.2.1"
},
"engines": {
"node": ">=0.8.0"
}
}
@andybluntish
Copy link
Author

Multiple file output achieved through exsl:document (http://exslt.org/exsl/elements/document/index.html).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment