I want to perform a series of operations on elements that matched the name "A" or "B". I'm thinking of something like this below, but it doesn't work.
<xsl:template match= " 'A' or 'B'" >
<!-- whatever I want to do here -->
</xsl:template>
Couldn't find the appropriate XSLT language reference for it. Please help! Thanks!!
I think it's more convenient to use this XPath
rather than
Try this:
See this page for details.
There are a few problems with this match pattern:
A template matches nodes, not strings. Therefore, the names of the elements to be matched should not be specified as quoted strings.
The XPath operator "or" acts on two boolean values, not on nodes. What is necessary here is another XPath operator -- the union operator "|".
Taking into account the above, one will correctly specify the template rule as:
Generally
A | B
is the right way to do this. But the pipe character is basically a union of two complete XPath expressions. It can be annoying to use it in a case like this:since you're repeating the entirety of the expression except for the one small piece of it's that changing.
In cases like this, it often makes sense to use a predicate and the
name()
function instead:This technique gives you access to a much broader range of logical operations. It's also (conceivably) faster, as the XPath navigator only has to traverse the nodes it's testing once.
The below information was gleaned from: http://www.cafeconleche.org/books/bible2/chapters/ch17.html#d1e2090
I will paraphrase, please search for the text "Using the or operator |" in that document.
The syntax is: