[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Getting the first p inside a div

Subject: Re: Getting the first p inside a div
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 19 Aug 2008 14:26:17 -0400
Re:  Getting the first p inside a div
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 ======================================================================

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.