Fun with E4X and XSL-as-templating-system

4 views
Skip to first unread message

Brett Zamir

unread,
Feb 12, 2009, 8:47:16 AM2/12/09
to same...@googlegroups.com
Piggybacking on Sameplace's spin-off seethrough templating system,
here's another E4X templating idea--using XSL for any who were interested:

(function () {
var NS_XSL = 'http://www.w3.org/1999/XSL/Transform';

window.for_each = function (select, content) {
return <xsl:for-each select={select}
xmlns:xsl={NS_XSL}>{content}</xsl:for-each>
}
window.value_of = function (select) {
return <xsl:value-of select={select} xmlns:xsl={NS_XSL}/>
}
})();

// Let's borrow the doc at http://www.w3schools.com/Xsl/xsl_for_each.asp
var xsl = <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
{for_each('catalog/cd',
<tr>
<td>{value_of("title")}</td>
<td>{value_of("artist")}</td>
</tr>
)}
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>;

alert(xsl); // gives document which could be converted to a string then
DOM for use within XSL conversion...

I think I like this approach for:
1) Using the visually distinct "{" and "}" as delimiters
2) allowing shorter expressions of the XSL sytnax (no writing XSL
namespaces and prefixes, no attribute names (unless desired as
properties on an object argument), and no closing tags)

I'm sure there could be a lot more interesting uses, like injecting or
making it work with JSON objects, etc.

I also added a bunch of links today to
https://developer.mozilla.org/En/JavaScript_templates if anyone was
interested...

best,
Brett

Reply all
Reply to author
Forward
0 new messages