Re: [xsl] Adding another layer of global styling

Subject: Re: [xsl] Adding another layer of global styling
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Sat, 26 Jul 2003 15:11:38 -0400
Chuck,

This is fun.

Discriminate two templates that you will use to handle your titles. One will handle their basic output based on their @level or @rend (what you already have); the other will provide the extra span wrapper around the Sanskrit ones.

To do this we first have to determine which will be the "outer" template. In your case, I think it's the one that will fire based on the setting of @lang. Your <span style="font-family: 'Titus'"> will wrap the spans and such you're already generating.

To achieve this we put it in a named template:

<xsl:template name="Sanskrit-wrap">
  <xsl:choose>
    <xsl:when test="@lang = 'sa'">
      <span style="font-family: 'Titus'">
        <xsl:apply-templates/>
      </span>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

But we don't want just to fire this -- we want it to apply not to the title, but to the result of processing our other template. We do this by extending the new template so it does not simply process down the tree as normally, but instead wraps a parameter we send it:

<xsl:template name="Sanskrit-wrap">
  <xsl:param name="contents">
    <xsl:apply-templates/>
  </xsl:param>
  <xsl:choose>
    <xsl:when test="@lang = 'sa'">
      <span style="font-family: 'Titus'">
        <xsl:copy-of select="$contents"/>
      </span>
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy-of select="$contents"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Notice we provide our parameter with a default, namel the same result-tree-fragment we get from applying templates to the children of the current node -- this will come in handy.

Now we have to fix our first template so it calls the second -- the trick here is, now we have a way to get our result *into* the Sanskrit wrapper --

<xsl:template match="title">
<xsl:call-template name="Sanskrit-wrap">
<xsl:with-param name="contents">
<xsl:choose>
<xsl:when test="ancestor::listBibl">
<xsl:choose>
<xsl:when test="@level='m'">
<span style="font-style:italic"><xsl:apply-templates/>. </span>
</xsl:when>
<xsl:when test="@level='j'">
<span style="font-style:italic"><xsl:apply-templates/>. </span>
</xsl:when>
<xsl:when test="@level='a'">&quot;<xsl:apply-templates/>.&quot;</xsl:when>
<xsl:when test="@level='u'">&quot;<xsl:apply-templates/>.&quot;</xsl:when>
<xsl:when test="@rend='bold'">
<span style="font-weight:bold"><xsl:apply-templates/></span>
</xsl:when>
<xsl:otherwise>
<span style="font-style:italic"><xsl:apply-templates/></span>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="@level='m'">
<span style="font-style:italic"><xsl:apply-templates/></span>
</xsl:when>
<xsl:when test="@level='j'">
<span style="font-style:italic"><xsl:apply-templates/></span>
</xsl:when>
<xsl:when test="@level='a'">&quot;<xsl:apply-templates/>&quot;</xsl:when>
<xsl:when test="@level='u'">&quot;<xsl:apply-templates/>&quot;</xsl:when>
<xsl:otherwise>
<span style="font-style:italic"><xsl:apply-templates/></span>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</xsl:template>


I haven't figured out what to call this technique, although I'm finding lots of uses for it. Note that you can also wrap other processing results in <span style="font-family: 'Titus'"> simply by sending in other parameters to the same template -- or none, since it defaults to xsl:apply-templates, which is what you usually want.

So imagine, if you will:

<xsl:template match="title">
  <xsl:call-template name="Sanskrit-wrap">
    <xsl:with-param name="contents">
      <xsl:choose>
        <xsl:when test="ancestor::listBibl">
           <xsl:choose>
             <xsl:when test="@level='m' or @level='j'">
                <xsl:call-template name="italicize">
                  <xsl:with-param name="contents">
                    <xsl:apply-templates/>. <xsl:text/>
                  </xsl:with-param>
                </xsl:call-template>
             </xsl:when>
             <xsl:when test="@level='a' or @level='u'">
                <xsl:call-template name="quote">
                  <xsl:with-param name="contents">
                    <xsl:apply-templates/>.<xsl:text/>
                  </xsl:with-param>
                </xsl:call-template>
             </xsl:when>
             <xsl:when test="@rend='bold'">
                <xsl:call-template name="embolden"/>
             </xsl:when>
             <xsl:otherwise>
                <xsl:call-template name="italicize"/>
             </xsl:otherwise>
           </xsl:choose>
         </xsl:when>
         <xsl:otherwise>
           <xsl:choose>
             <xsl:when test="@level='a' or @level='u'">
               <xsl:call-template name="quote"/>
             </xsl:when>
             <xsl:otherwise>
               <xsl:call-template name="italicize"/>
             </xsl:otherwise>
           </xsl:choose>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:with-param>
   </xsl:call-template>
</xsl:template>

Though in the big scheme of things, this may not be that huge an improvement. (It depends on what else is going on.)

Ask again if you need more clarification.

Cheers,
Wendell

At 01:20 AM 7/26/2003, you wrote:
I have a set of combinations for various renderings of <title> according
to attributes and XPath position that handle titles through in my
articles and other documents and so forth that I write using TEI
(present code shown below).

I now want to add one more universal style application that should also
apply in other attribute situations when necessary: I want to apply the TITUS font
to all cases of <title lang="sa"> (Language = Sanskrit). I can't add
another <xsl:template match="title">, and I can't simply add it as an
choose/if/when option, since it is not simply an option, but a
document-wide application. I guess there must be a way of doing this.


TIA

Chuck

___&&__&_&___&_&__&&&__&_&__&__&&____&&_&___&__&_&&_____&__&__&&_____&_&&_ "Thus I make my own use of the telegraph, without consulting the directors, like the sparrows, which I perceive use it extensively for a perch." -- Thoreau


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread