0

I've got an XML in this vein:

<style name='Danger'/>
<style name='Danger_2'/>
<style name='Danger_3'/>
<style name='Warning'/>
<style name='Warning_2'/>
<style name='Warning_3'/>
<style name='Body'/>

I'm trying to process all styles whose name contains "Danger" or "Warning". Naive approach that doesn't work:

<xsl:template match="style[contains(@name, 'Danger|Warning')]">

I'm aware you can do this in XSLT 2.0 with exact matches:

"style[@name=('Danger','Warning')]"

Is there a solution for 'contains' matches?

This related question has an interesting solution: 'Use a pipe (or other appropriate character) delimited string', but despite the use of 'contains' that only works for exact matches (so it'll match 'Danger' but not 'Danger_1').

Hobbes
  • 1,964
  • 3
  • 18
  • 35

2 Answers2

3

With XSLT/XPath 2.0 and later there is the matches function (http://maxtoroq.github.io/xpath-ref/fn/matches.html) with regular expression support style[matches(@name, 'Danger|Warning')].

Additionally you can also use style[some $s in ('Danger','Warning') satisfies contains(@name, $s)], that is also possible with XSLT/XPath 2.0 and later.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
1

Have you tried the following?

<xsl:template match="style[contains(@name, 'Danger') or contains(@name, 'Warning')]">

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76