Re: [xsl] Ignoring ambiguous matches

Subject: Re: [xsl] Ignoring ambiguous matches
From: Graydon <graydon@xxxxxxxxx>
Date: Wed, 12 Feb 2014 20:35:25 -0500
On Thu, Feb 13, 2014 at 01:06:25AM +0000, Ihe Onwuka scripsit:
> I am about to write several template rules that will all match the
> same node but will each apply (or try to apply) a different edit.
> 
> I don't care the order in which these are applied as long as each gets
> a shot at applying it's edit.
> 
> I was wondering whether I can just ignore the ambiguous match warnings
> and be confident that everything is A - ok or whether I have to
> diligently invent template priorities to prevent that.

If you need everything to match, it's not safe to ignore ambiguous rule
match warnings; you're going to get only one of the rules matching,
hopefully the last one.

For XSLT 2.0, last thing in section 6.4 of the spec:

"It is a recoverable dynamic error if the conflict resolution algorithm
for template rules leaves more than one matching template rule. The
optional recovery action is to select, from the matching template rules
that are left, the one that occurs last in declaration order."

Why not find the node and do everything you need to do to that node
inside that template.

Usually something like

<xsl:variable name="pass1">
    <xsl:apply-templates mode="pass1" select="."/>
</xsl:variable>

<xsl:variable name="pass2">
    <xsl:apply-templates mode="pass2" select="$pass1"/>
</xsl:variable>

and eventually

<xsl:sequence select="$passn"/>

Whatever transform you want at each step has to be present as templates
with the appropriate modes.

Next match for multiple templates is tricky, you have to be sure all the
templates have exactly the same priority or set the priorities carefully
and this, well, there's a reason one generally prefers to let the
processor deal with priority.

-- Graydon

Current Thread