public
Created

Basic client-side XML/XSLT setup

  • Download Gist
contact.xml
XML
1 2 3 4 5 6 7 8 9 10 11
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xml" href="transform.xslt"?>
<data>
<page id="contact">
<p>
Our contact info:
</p>
<name>Moe Zart</name>
<email>moe@moesbar.com</email>
</page>
</data>
front.xml
XML
1 2 3 4 5 6 7 8 9 10
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xml" href="transform.xslt"?>
<data>
<page id="front">
<header>The Front Page</header>
<p>
You can contact us from the <link>contact</link> page.
</p>
</page>
</data>
transform.xslt
XSLT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
 
<!-- Note: about:legacy-compat is the XSLT version of the HTML5 DOCTYPE -->
<xsl:output method="html" indent="yes"
omit-xml-declaration="yes"
doctype-public="about:legacy-compat"
/>
<xsl:template match="/">
<html>
<head>
<title>Demo</title>
</head>
<body>
<xsl:apply-templates select="data/page" />
</body>
</html>
</xsl:template>
<xsl:template match="page">
<article>
<xsl:apply-templates />
</article>
</xsl:template>
<xsl:template match="header">
<h1>
<xsl:apply-templates />
</h1>
</xsl:template>
<xsl:template match="p">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="link">
<a href="{.}.xml">
<xsl:apply-templates />
</a>
</xsl:template>
<xsl:template match="email">
<div class="email">
<a href="mailto:{.}"><xsl:value-of select="." /></a>
</div>
</xsl:template>
<xsl:template match="name">
<div class="name">
<xsl:value-of select="." />
</div>
</xsl:template>
 
</xsl:stylesheet>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.