Subject: Re: Getting the first p inside a div
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 19 Aug 2008 14:26:17 -0400
|
At 12:25 PM 8/19/2008, you wrote:
<xsl:template match="note//p">
<p class="note_p">
<xsl:if test="generate-id() = generate-id(ancestor::note/
descendant::p[1])">
<xsl:apply-templates select="ancestor::note/label"/>
</xsl:if>
<xsl:apply-templates/>
</p>
</xsl:template>
What this does:
* Collapse the templates together for all p elements inside note,
rather than splitting a template out for the first p. This is
clearer, allows better code reuse, and is more robust.
I agree (about the robustness), and it works perfectly.
Interestingly, it's also an exception to the general principle that
more templates are usually better. In this case, I think that's a
consequence of how the rules limit what expressions can be used as
match patterns (so, for example, the pattern "note/descendant::p[1]"
is illegal), plus the fact that in this case the special treatment
isn't exclusive of the general treatment (both sorts of p become
p[@class='note_p']).
As an aside -- this code assumes that you'll never have notes nested
inside your notes. If you do, amend the test to look at
ancestor::note[1]/descendant::p[1] (and maybe watch out for more edge cases).
Cheers,
Wendell
======================================================================
Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc. http://www.mulberrytech.com
17 West Jefferson Street Direct Phone: 301/315-9635
Suite 207 Phone: 301/315-9631
Rockville, MD 20850 Fax: 301/315-8285
----------------------------------------------------------------------
Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================
|