Re: [xsl] global language parameter

Subject: Re: [xsl] global language parameter
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Mon, 15 Feb 2010 10:35:38 +0000
Hi,

Slightly off-topic but worth pointing out maybe::

>        <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>
>        <xsl:when test="@rend='blockquoteNested1'">
>          <xsl:text>margin-left:15mm; margin-top:0.5em; </xsl:text>
>          <xsl:text> text-indent:0mm; margin-right:2mm;</xsl:text>
>          <xsl:text>margin-bottom:0.5em;line-height:7mm;</xsl:text>
>        </xsl:when>
>        <xsl:when test="@rend='verseline'">
>          <xsl:text>margin-left:12mm; margin-top:0em;</xsl:text>
>          <xsl:text>margin-bottom:0em;</xsl:text>
>          <xsl:text> text-indent:0mm; margin-right:12mm ; </xsl:text>
>          <xsl:text>line-height:7mm;</xsl:text>
>        </xsl:when>
>        <xsl:when test="@rend='passage'">
>          <xsl:text>margin-left:5%; margin-right:5%;
> margin-top:0.7em;</xsl:text>
>          <xsl:text> text-indent:0mm; line-height:8mm;</xsl:text>
>          <xsl:text>margin-bottom:0.7em;</xsl:text>
>        </xsl:when>
>        <xsl:when test="@rend='centered'">
>          <xsl:text>text-align: center; margin-top: 0; margin-bottom:
> 0;</xsl:text>
>          <xsl:text>line-height:7mm;</xsl:text>
>        </xsl:when>
>        <xsl:when test="@rend='note'">
>          <xsl:text>font-size:90%; margin-left:0; margin-top:1em;</xsl:text>
>          <xsl:text>line-height:6mm; margin-bottom:0;
> text-indent:0mm;</xsl:text>
>        </xsl:when>
>        <xsl:when test="@rend='hangInd'">
>          <xsl:text>text-indent:-10mm;margin-left:10mm;</xsl:text>
>        </xsl:when>
>        <xsl:when test="@rend='indHangInd'">
>          <xsl:text>text-indent:-10mm;margin-left:15mm;</xsl:text>
>        </xsl:when>
>        <xsl:when test="@rend='plainTight'">
>          <xsl:text>margin-left:5%; margin-top:0;
margin-bottom:0;</xsl:text>
>          <xsl:text> text-indent:0mm; line-height:6mm;</xsl:text>
>        </xsl:when>
>        <xsl:when test="@rend='abstract'">
>          <xsl:text>font-size:80%; line-height:5mm;
> margin-bottom:0;</xsl:text>
>          <xsl:text>margin-top:0;</xsl:text>
>          <xsl:text> margin-left:10%; margin-right:10%;</xsl:text>
>        </xsl:when>
>      </xsl:choose>
...
>      <!--what is the language component-->
>      <xsl:choose>
>        <xsl:when test="lang('sa')">font-family: 'Times Ext Roman', Times
New
> Roman;</xsl:when>
>        <xsl:when test="lang('zh')">font-family:MingLiU, Mincho,Batang,
> Simsun;</xsl:when>


Instead of repeatedly using inline styles for each value of @rend like
that, just use a class with named classes of styles:

class="standard_indent standard_line_height standard_margin lang_SA"

then in the separate CSS file, define those classes of style once:

.standard_indent {
  text-indent:12mm;
}

.standard_line_height {
  line-height:9mm;
}

.standard_margin {
  margin-top: 0;
  margin-bottom: 0;
}

.lang_SA {
  font-family: 'Times Ext Roman', Times New Roman;
}

The advantages are that you only have one place to change when you
decide to change something that applies to several areas, but more
importantly someone who knows nothing about XSLT but lots about HTML
and CSS can make that change.

To get back to XSLT, another good practice tip is use templates
instead of 1 big choose/when:

<xsl:template match="@rend[. eq 'indent']">
  <xsl:text>standard_indent standard_margin ...</xsl:text>
</xsl:template>

along with:

<p>
  <xsl:attribute name="class">
    <xsl:apply-templates select="@rend"/>

...as this allows individual templates to be overridden by a
stylesheet that imports it, rather than having to override the entire
"add_style" template effectively making a big copy of it with 1 small
change, forcing all future changes to the common code to be made in
two places.



cheers

--
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread