More mojo from Spring CMS
Tuesday, December 13, 2005I've written before about Spring's mojo and here are some more examples. I needed some consistency with the way blockquotes were displayed. I wanted to create a source link for each blockquote but didn't want to have to type it in every time. Also I wanted to create a linkography for each page from all the outbound links.
Sourcing Blockquotes
Having been inspired by Tantek's Meaningful XHTML presentation I had already added cite attributes to all my blockquotes. I knew I could pull out the cite attribute and create a link under the blockquote using XSLT but I needed some text for the link. So I decided to add a title attribute to each blockquote. Thus I can use this XSL:
<xsl:template match="blockquote">
<blockquote>
<xsl:copy-of select="@cite"/>
<xsl:copy-of select="@title"/>
<xsl:value-of select="./descendant-or-self::*"/>
</blockquote>
<xsl:if test="string(@cite)">
<p class="quote-src">
<xsl:text>#Source: </xsl:text>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@cite"/>
</xsl:attribute>
<xsl:choose>
<xsl:when test="string(@title)">
<xsl:value-of select="@title"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@cite"/>
</xsl:otherwise>
</xsl:choose>
</a>
</p>
</xsl:if>
</xsl:template>
The above XSL has been amended to put the source link outside of the blockquote, since the blockquote area is only for quoted text.
to transform a blockquote from this:
<blockquote
cite="http://link.to.somewhere/"
title="An interesting article">
"An interesting quote"
</blockquote> into this:
"An interesting quote"
All the XSL does is rewrite the blockquote as an exact copy of itself, except when it finds a cite attribute where it'll append a paragraph with a link, using the title attribute, if available, otherwise the URL itself, for the link text.
Generating a Linkography
I've also added some XSL to grab all a tags with an href starting with 'http://' and make a linkography for each page using the title attribute for a description. Here's the XSL:
<xsl:if test="contains(@href, 'http://')">
<li>
<a>
<xsl:copy-of select="@href"/>
<xsl:value-of select="."/>
</a><xsl:text> </xsl:text><xsl:value-of select="@title"/>
</li>
</xsl:if> And you can see the result at the end of the page.
Results
Pretty neat huh? It means I can really go to town using XHTML markup and transform it in interesting ways without any need to alter the CMS. That's the benefit of using XML and XSLT.
No comments yet