Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: How to represent OR in a "match" attribute?

Tags:

xml

xslt

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!!

like image 473
tomato Avatar asked Mar 05 '09 20:03

tomato


People also ask

What is match in XSLT?

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).

What does @* mean in XSLT?

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() .

What is regex in XSLT?

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.

How do I comment out a line in XSLT?

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.


2 Answers

Try this:

<xsl:template match= "A | B" > 

See this page for details.

like image 88
David Avatar answered Oct 11 '22 14:10

David


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.

like image 27
Robert Rossney Avatar answered Oct 11 '22 15:10

Robert Rossney