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!!
The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).
The XPath expression @* | node() selects the union of attribute nodes ( @* ) and all other types of XML nodes ( node() ). It is a shorthand for attribute::* | child::node() .
The regex-group() function names which matched string you want to use inside of the xsl:matching-substring element; pass it a 1 to get the first, a 2 to get the second, and so forth. The example above uses it to plug the three matched values inside new city, state, and zip elements created for the output.
A <! -- --> comment can be produced in the output with the comment element. The content of the comment element is used as the contents of the output comment. Literal comments in a template are assumed to be a comment for the XSLT stylesheet, and are ignored, so a comment in the output can't be produced that way.
Try this:
<xsl:template match= "A | B" >
See this page for details.
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:
/red/yellow/blue/green/gold | red/orange/blue/green/gold
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:
/red/*[name() = 'yellow' or name()='orange']/blue/green/gold
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With