<a>
<x/>
<m/>
<y/>
<m/>
</a>
Inside a template that matches ‘a’, I want to match (first) any ‘m’s before ‘y’, and then
separately any ‘m’s after ‘y’.
<xsl:apply-templates select="./m[following::y]"/>
is what I thought of, but I can’t get it to work, and, further, I can’t see how to prevent the template that matches on ‘m’ from being applied in the normal flow as well as in the particular place i want to insert the m-related content.
Your template looks OK, but are you sure you want to use
following? For example, this template:…applied to the following XML:
…outputs the following result:
The first
apply-templatesmatches<m>no match</m>becausefollowingincludes all nodes that are after the context node in document order, which includes the nested<y/>.The second template matches only siblings.
For completeness, I’ll add the following template, which matches only those
<m>nodes whose immediate following sibling is a<y>:This template outputs the following, given the above XML: