Am I correct in thinking that an apply-templates
declaration should match all templates which could possibly be applied for a selection?
For example, given the following xml fragment:
<doc>
<foo bar="1" baz="2">boz</foo>
</doc>
and the following stylesheet:
<xsl:template match="/">
<xsl:apply-templates select="foo" mode="xyz" />
</xsl:template>
<xsl:template mode="xyz" match="foo[bar='1']">
abc
</xsl:template>
<xsl:template mode="xyz" match="foo[baz='2']">
def
</xsl:template>
I would expect the output to be:
abc
def
Is this correct?
If you did want your templates to match for both attributes, then you would just need to adjust the
match
XPATH to select the attributes and put the relationship tofoo
in the predicate; rather than having two conflicting templates matching on thefoo
element with the same specificity(which have the same priority).If you fix that XSLT code (you have some filter problems) and ran it, you should see:
Why?
<xsl:apply-templates />
will match first template which satisfies your match condition. If you have two templates, you should to differ than by using that<xsl:apply-templates>
mode
attribute, or by using<xsl:template>
priority
attribute:No, you don't get both outputs, as only one template will be selected. See this page for the rules on conflict resolution if there are multiple possible templates.
After fixing your stylesheet (similarly to how Rubens did it, but with same modes), this usually will result in the last template in your xslt file to be applied, so the output will be
def
. This is because the two templates have the same priority and if your xslt processor does not stop with an error the standard requires it to apply the last one: