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: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 20 Aug 2009 12:41:06 -0400
At 2009-08-20 15:20 +0100, Kate Busch-Petersen wrote:
Brilliant! That's exactly what I'm looking for.

I've popped some line breaks in to make it easier for the next developer, tested it and am really happy with it:

      <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)]
                           "/>

Whether the processor optimizes your expression to the above expression is up to the processor, but the above does only one comparison per predicate not two, so for some processors my suggestion may in fact be executed more quickly. There are many cases in my OASIS code list work where I use this "not(x!=y)" idiom very successfully.

The principle I bring up in class is that a node set comparison with an empty node set operand returns false. If it is not empty all members are tested with the other operand until a true is found, otherwise a false is returned when all tests are exhausted.

So if there is no AuthorId, then the blog element is selected because the node set is empty making the inside of the not() false and the outside of not() true. If there is an AuthorId and it is equal to author_id, the inside of not() is false, so the outside of not() is true, and the blog element is selected. But if there is an AuthorId and it is not equal to author_id, the inside of not() is true, so the outside of not() is false, and the blog element is not selected.

I hope this is considered helpful and not critical of your work.

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


-- Interested in these classes? http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread