Skip to content

Instantly share code, notes, and snippets.

@nils-werner
Created December 22, 2011 17:00
Show Gist options
  • Save nils-werner/1510998 to your computer and use it in GitHub Desktop.
Save nils-werner/1510998 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<link rel="stylesheet" href="workspace/assets/style.css"/>
</head>
<body>
<div id="head">
<h1>Title</h1>
</div>
<div id="package">
<div id="content">
<h2>Blog</h2>
<div datasource="blog-articles">
<div class="article">
<h3 value="title">Title</h3>
<p copy="body">
Lorem Ipsum
</p>
</div>
</div>
<div id="comments">
<h3>Comments</h3>
<div datasource="article-comments">
<p value="text">
Commenttext
</p>
</div>
</div>
<div id="sidebar">
<h3>
All Articles
</h3>
<ul>
<div datasource="blog-article-index">
<li value="title">Something</li>
</div>
</ul>
</div>
</div>
</div>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<x:stylesheet version="1.0"
xmlns:x="http://www.w3.org/1999/XSL/Transform"
xmlns:xsl="http://www.w3.org/1999/XSL/TransformAlias">
<x:output method="xml"
encoding="UTF-8"
indent="yes" />
<x:namespace-alias stylesheet-prefix="xsl" result-prefix="x"/>
<x:template match="*">
<x:element name="{name()}">
<x:apply-templates select="* | @* | text()" />
</x:element>
</x:template>
<x:template match="@*">
<x:attribute name="{name()}">
<x:value-of select="."/>
</x:attribute>
</x:template>
<x:template match="@datasource | @value | @copy" />
<x:template match="/">
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/TransformAlias">
<xsl:output method="xml"
encoding="UTF-8"
indent="yes" />
<x:apply-templates />
<x:apply-templates select="//*[@datasource]" mode="templates" />
</xsl:stylesheet>
</x:template>
<x:template match="*[@datasource]">
<xsl:apply-templates select="{@datasource}/entry" />
</x:template>
<x:template match="*[@value]/text()">
<xsl:value-of select="{../@value}" />
</x:template>
<x:template match="*[@copy]">
<xsl:copy-of select="{@copy}/*" />
</x:template>
<x:template match="*[@datasource]" mode="templates">
<x:text>
</x:text>
<xsl:template match="{@datasource}/entry">
<x:apply-templates />
</xsl:template>
</x:template>
</x:stylesheet>
@nils-werner
Copy link
Author

The above code shows an improvised bare-bone inline templating language. Instead of creating templates and matching nodes you'd specify the Data Sources you want data to pull from using the datasource attribute in any element. Inside those elements you can use the value and copy attributes to pull single field values into your markup.

For example:

<div datasource="blog-articles">
    <div class="article">
        <h3 value="title">Title</h3>
        <p copy="body">
            Lorem Ipsum
        </p>
    </div>
</div>

would be translated into

<xsl:template match="blog-articles/entry">
    <div class="article">
        <h3><xsl:value-of select="title"/></h3>
        <xsl:copy-of select="body"/>
    </div>
</xsl:template>

and

<xsl:apply-templates select="blog-articles/entry" />

All value or copy nodes are being replaced with actual content so it's possible to use blind text in them.

@nils-werner
Copy link
Author

To try this out simply download the code and run it using

xsltproc master.xsl master.xml

The returned XML is an XSLT stylesheet that can be used with any XSLT processor (including Symphony).

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