RE: [xsl] Avoiding multiple "apply-templates" by creating one variable for the clauses. Is it possible?

Subject: RE: [xsl] Avoiding multiple "apply-templates" by creating one variable for the clauses. Is it possible?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 20 Aug 2009 17:33:02 -0400
Hi,

Another approach altogether is to "push" the logic through templates:

<xsl:templates select="//blog"/>

... and then ...

<xsl:template match="blog[$AuthorId][not(author_id=$AuthorId)]" priority="2.1"/>

<xsl:template match="blog[$CategoryId][not(category_id=$CategoryId)]" priority="2.2"/>

<xsl:template match="blog">... process any blogs that fall through ...</xsl:template>

Note:

1. I've used the long logic here (not Ken's more succinct version), since in the world I work in, articles can have multiple authors and categories. (Dunno about your blogs.)

2. The explicit priority settings are to guard against multiple templates matching.

3. The approach only works if $AuthorId and $CategoryId are globally scoped. (And in 2.0 where you can use them in matches.)

4. In general, opinions vary on whether this is more or less legible and maintainable.

Cheers,
Wendell

At 01:18 PM 8/20/2009, Mike wrote:

> > > > <xsl:apply-templates select="//blog > > [not($AuthorId) or (author_id = $AuthorId)] > > [not($CategoryId) or (category_id = $CategoryId)] > > "/> > > I guess I'll finally drop in and add another suggestion since > I've been waiting for one of my XSLT students to bring it up > but I don't see any of them doing so. > > If you are looking for compactness, I believe the above > expression can be reduced to the following in both XSLT 1.0 > and XSLT 2.0: > > <xsl:apply-templates select="//blog > [not($AuthorId != author_id)] > [not($CategoryId != $CategoryId)] > "/> >

I'm not convinced about this equivalence. If $AuthorId is "Mike", and
author_id is an empty sequence, then

(a) not($AuthorId) or (author_id = $AuthorId) ==> false

(b) not($AuthorId != author_id) ==> true

So if the optimizer does this conversion for you, it's wrong!

But it's probably safe if author_id will always be a singleton node-set.





====================================================================== 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