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 |
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>.
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.
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Matching the first text e, J.Pietschmann | Thread | Re: [xsl] Matching the first text e, Wendell Piez |
Re: [xsl] Aargh - wrong template ap, J.Pietschmann | Date | Re: [xsl] XML View in IE 6, Neelima Adusumilli |
Month |