[xsl] Re: Matching the first text element in a subtree

Subject: [xsl] Re: Matching the first text element in a subtree
From: Martin Holmes <mholmes@xxxxxxx>
Date: Wed, 19 Apr 2006 16:14:52 -0700
HI Wendell,

This makes sense, and I'll stash it away for future use. In the end,
after much work today, we determined that it's virtually impossible to
decide where or when a drop-cap should appear without human
intervention. Some texts begin with punctuation such as open quotes:
should the quote be drop-capped, or the subsequent letter, and if the
latter, what should happen to the quote? Some articles begin with a
block-indented verse quotation; what then? In the end, we've decided to
pollute our XML with an explicit

<hi rend="DropCap">F</hi>irst word

where we think it works best. Examining previous print editions of the
journal we're now working on, I'm convinced that the actual texts were
edited precisely to make their first sentences amenable to elegant
drop-caps, since in every case they are unproblematic (whereas two out
of the three articles marked up for our edition so far present
difficulties).

Cheers,
Martin

Wendell Piez wrote:
Hi Martin,

In order to avoid the monster match pattern, I'd probably relent from my usual habits and put part of the testing here into an explicit conditional:

<xsl:template match="div0//text()[normalize-space()]">
  <xsl:choose>
     <xsl:when test="not(ancestor-or-self::node()[ancestor::div0]/
                         preceding-sibling::node()[normalize-space()]">
       <!-- basically Ken's test, only broken up -->
       <wrapper>
         <xsl:value-of select="substring(.,1,1)"/>
       </wrapper>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="substring(.,1,1)"/>
     </xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="substring(.,2)"/>
</xsl:template>

Ordinarily, I'm inclined to think that choose/when/otherwise is less elegant than doing the selection entirely in the match pattern. This is an exception, since I think that (for maintenance purposes) this is easier both to understand, and to comment with whatever explanation is called for to understand it fully.

Note that I've also varied slightly from Ken's solution in not requiring the text node to be in a paragraph. (I missed whether that was part of the original requirement.)

It's not a radical improvement but depending on your feelings about such things you may like the balance it strikes a bit better.

Cheers,
Wendell

Current Thread