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

Subject: Re: [xsl] Matching the first text element in a subtree
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 18 Apr 2006 20:54:43 -0400
At 2006-04-19 02:37 +0200, J.Pietschmann wrote:
Martin Holmes wrote:
I'm attempting (using XSL 1.0 and Saxon 6.5.5) to do drop-caps by matching the first text element in the first paragraph of the second div0 in a TEI document. Where the text node is a direct child of the p tag, this works:
<xsl:template match="div0[2]/p[1]/text()[1]">
However, where another tag intervenes in the tree, this fails:
<div0>
<p>
<title>Blah</title> was published in...
</p>
</div0>
Here, the first text node is the one following the title tag,

Well, unless you strip whitespace generously, the first text node would be the whitespace just after the <p>.

Indeed it would.


 So what I want to do is select the first text node which is a
descendant (rather than a direct child) of the p tag.

Try <xsl:template match="(div0[2]/p[1]//text())[1]"> note the additional parenthesis (beware, untested). Be sure to combine this with proper whitespace stripping.

Unfortunately, while using parentheses in select= attributes is fine, doing so is disallowed in match= attributes.


The code below appears to work in a few limited situations of the included test ... but I think it would encompass most combinations (I've just run out of time to do more this evening). My thought is that what has to be true about the node being tested is that neither it nor any of the ancestors up to the div0 can have any preceding nodes that have any text value.

To mimic what you need, I'm putting the first character of the first non-white-space-only text descendent of a div0 in parentheses.

It isn't pretty, but I hope this helps. Perhaps someone else on the list has a more elegant solution.

. . . . . . . . . . . Ken

T:\ftemp>type martin.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="div0/p//text()[normalize-space()][1]
                     [not(ancestor-or-self::node()[ancestor::div0]/
                          preceding-sibling::node()[normalize-space()])]">
  <xsl:text/>(<xsl:value-of select="substring(.,1,1)"/>)<xsl:text/>
  <xsl:value-of select="substring(.,2)"/>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>type martin.xml
<divs>
  <div0>
    <p>
      <title>Blah</title> was published in...
      <emph>Other text</emph> Yet more text
    </p>
    <p>
      <title>Second P</title> was published in...
      <emph>Other text</emph> Yet more text
    </p>
  </div0>
  <div0>
    <p>Another
      <title>Blah</title> was published in...
      <emph>Other text</emph> Yet more text
    </p>
    <p>Yet Another
      <title>Blah</title> was published in...
      <emph>Other text</emph> Yet more text
    </p>
  </div0>
</divs>
T:\ftemp>xslt martin.xml martin.xsl con
<?xml version="1.0" encoding="utf-8"?><divs>
  <div0>
    <p>
      <title>(B)lah</title> was published in...
      <emph>Other text</emph> Yet more text
    </p>
    <p>
      <title>Second P</title> was published in...
      <emph>Other text</emph> Yet more text
    </p>
  </div0>
  <div0>
    <p>(A)nother
      <title>Blah</title> was published in...
      <emph>Other text</emph> Yet more text
    </p>
    <p>Yet Another
      <title>Blah</title> was published in...
      <emph>Other text</emph> Yet more text
    </p>
  </div0>
</divs>
T:\ftemp>


-- Registration open for XSLT/XSL-FO training: Wash.,DC 2006-06-12/16 Also for XML/XSLT/XSL-FO training:Birmingham,England 2006-05-22/25 Also for XSLT/XSL-FO training: Copenhagen,Denmark 2006-05-08/11 World-wide on-site corporate, govt. & user group XML/XSL training. G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Aug'05 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread