Re: [xsl] Re: [saxon] template match dependent on parent

Subject: Re: [xsl] Re: [saxon] template match dependent on parent
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxx>
Date: Fri, 31 Jan 2014 11:48:23 -0500
Hi,

I would probably go with match patterns:

<xsl:template match="parent[@attr eq 'case1']/a"> ... </xsl:template>

<xsl:template match="parent[@attr eq 'case2']/a"> ... </xsl:template>

<xsl:template match="a"> [ fallback case ]</xsl:template>

In the real world, priorities might have to be set (although the
default priorities work well enough for the example).

As for performance, I do not assume this will perform less well than
an xsl:choose or other conditional 'pull' -- this surely depends on
the processor and how well it optimizes template matching.

As a maintainer, I generally prefer template-driven conditionals
because they expose the logic better and make the XSLT more legible --
at least to someone who understands the processing model. (And if a
future maintainer doesn't understand that, this gives them a chance to
learn. :-)

On the other hand, much also depends on exactly what the requirements
are for the 'a' in these two cases. If they are more similar than
different, there may be other ways to refactor.

Cheers,
Wendell


On Thu, Jan 30, 2014 at 11:41 PM, Ihe Onwuka <ihe.onwuka@xxxxxxxxx> wrote:
> No. It just moves the test from the match pattern to inside the
> template rule and it's more verbose.
>
> Giving it further thought I think this is a case for a pull processing.
>
> <xsl:template match="onTheParentOFA">
>   <xsl:if test="@attr eq 'datasource1'">
>      <xsl:for-each select="a">
>         etc
>      </xsl:for-each>
>   </xsl:if>
>     <xsl:if test="@attr eq 'datasource2'">
>      <xsl:for-each select="a">
>         etc
>      </xsl:for-each>
>   </xsl:if>
> </xsl:template>
>
> and the test for the datasource only gets done once.
>
> Apologies I just realised I sent this to the wrong mailing list.
> Correcting that now.
>
> On Fri, Jan 31, 2014 at 4:20 AM, Lizzi, Vincent
> <Vincent.Lizzi@xxxxxxxxxxxxxxxxxxxx> wrote:
>> Would this be more efficient?
>>
>> <xsl:template match="a[parent::*/@attr]">
>> <xsl:choose>
>> <xsl:when test="contains(parent::*/@attr,'datasource1')">
>>    blah
>>    blah
>> </xsl:when>
>> <xsl:when test="contains(parent::*/@attr,'datasource2')">
>>    blah
>>    blah
>> </xsl:when>
>> <xsl:otherwise>
>>    blah
>>    blah
>> </xsl:otherwise>
>> </xsl:choose>
>> </xsl:template>
>>
>>
>> -----Original Message-----
>> From: Ihe Onwuka [mailto:ihe.onwuka@xxxxxxxxx]
>> Sent: Thursday, January 30, 2014 11:13 PM
>> To: Mailing list for the SAXON XSLT and XQuery processor
>> Subject: [saxon] template match dependent on parent
>>
>> So I want to match on <a> elements.
>>
>> What I want to do in the template rule however is dependent on an attribute
in the parent of the <a> element that conveys the data source.
>>
>> I could do
>>
>>  <xsl:template match="a[contains(../@attr,'datasource1')]">
>>    blah
>>    blah
>> </xsl:template>
>>
>>  <xsl:template match="a[contains(../@attr,'datasource2'])">
>>    blah
>>    blah
>> </xsl:template>
>>
>> and it should work, but that means I am testing the contains condition on
every iteration of <a> element but it will return the same value for every
iteration and hence is unsatisfactory.
>>
>> Any advances on this.
>>
>>
-----------------------------------------------------------------------------
-
>> WatchGuard Dimension instantly turns raw network data into actionable
security intelligence. It gives you real-time visual feedback on key security
issues and trends.  Skip the complicated setup - simply import a virtual
appliance and go from zero to informed in seconds.
>>
http://pubads.g.doubleclick.net/gampad/clk?id=123612991&iu=/4140/ostg.clktrk
>> _______________________________________________
>> saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/saxon-help
>



--
Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables
_____oo_________o_o___ooooo____ooooooo_^

Current Thread