Mike Austin's Blog

Friday, December 14, 2007

Fun with XSLT

I played around with XSLT a while ago (it's been around a long time), and recently started looking at it again. It's nifty how declarative it is, for example you can set the attribute of a div whether it is an even or odd numbered child (only snippets for brevity):
<friends>
<friend name="Bob"/>
<friend name="Ray"/>
<friend name="Joe"/>
</friends>

<xsl:template match="friend[position() mod 2 = 1]">
<xsl:copy>
<xsl:attribute name="class">odd</xsl:attribute>
</xsl:copy>
</xsl:template>

<div class="odd">Bob</div>
<div>Ray</div>
<div class="odd">Joe</div>
It's like aspect oriented programming, where you can define rules that apply to any parts of the document.

0 Comments:

Post a Comment

<< Home