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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried the following?

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

1 Comment

I'd prefer a shorter solution if one exists (need to match 20 strings, not 2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.