Re: [xsl] global language parameter

Subject: Re: [xsl] global language parameter
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 16 Feb 2010 12:40:09 -0500
Dear Charles,

In addition to everything you've done, I would suggest you now consider refactoring this logic into a dedicated mode.

That is, instead of

At 04:02 AM 2/15/2010, you wrote:
<xsl:template name="add-style">
<xsl:attribute name="style">
<!--what is the margin component-->
<xsl:choose>
<xsl:when test="@rend='indent'">
<xsl:text>text-indent:12mm; margin-top: 0;margin-bottom: 0; </xsl:text>
<xsl:text>line-height:9mm;</xsl:text>
</xsl:when>
<xsl:when test="@rend='plain'">
<xsl:text>text-indent:0;margin-left:0; margin-top:0;</xsl:text>
<xsl:text>margin-bottom: 0; line-height:9mm;</xsl:text>
</xsl:when>
<xsl:when test="@rend='blockquote'">
<xsl:text>text-indent:0;margin-left:12mm; margin-top:1em;</xsl:text>
<xsl:text>margin-bottom: 1em; margin-right: 2em;</xsl:text>
<xsl:text>line-height:9mm;</xsl:text>
</xsl:when>

... etc ... you have


<xsl:template match="*[@rend='indent']" mode="add-style">
  <xsl:text>text-indent:12mm; margin-top: 0;margin-bottom: 0; </xsl:text>
  <xsl:text>line-height:9mm;</xsl:text>
</xsl:template>

<xsl:template match="*[@rend='plain']" mode="add-style">
  <xsl:text>text-indent:0;margin-left:0; margin-top:0;</xsl:text>
  <xsl:text>margin-bottom: 0; line-height:9mm;</xsl:text>
</xsl:template>

<xsl:template match="*[@rend='blockquote']" mode="add-style">
  <xsl:text>text-indent:0;margin-left:12mm; margin-top:1em;</xsl:text>
  <xsl:text>margin-bottom: 1em; margin-right: 2em;</xsl:text>
  <xsl:text>line-height:9mm;</xsl:text>
</xsl:template>

Then it's simply a matter of having

<xsl:attribute name="style">
  <xsl:apply-templates select="." mode="add-style"/>
</xsl:attribute>

I would argue this is a bit cleaner and more extensible, in addition (perhaps) to performing better, than a long choose/when/otherwise.

You can go further with this also. For example, a dedicated mode could add the font styling based on language, too, using the lang() function to discriminate matches. And you can distinguish easily between properties added only to block elements (margins and such) vs properties added to both blocks and inlines.

Finally, you might consider abstracting the regularities between the styles -- in this case, probably into named templates, which are good when boilerplate is really boilerplate.

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread