Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT 2.0 - Template Matching With Contains()

I'm wondering if it is possible to write a template match with the contains() function.

I have a document that has multiple elements that need to be renamed to a common element. All of the following need to be renamed to just OP: OP1.2, OP7.3, OP2.4, OP5.6`, etc.

like image 976
Katie Avatar asked Feb 17 '11 23:02

Katie


1 Answers

Yes, you can use contains() inside of a predicate filter in the match criteria for elements.

<xsl:template match="*[contains(local-name(),'OP')]>
  <OP>
    <xsl:apply-templates select="@*|node()"/>
  </OP>
</xsl:template>

You could also use starts-with()

*[starts-with(local-name(),'OP')]

If you are using XSLT 2.0 you could use the matches() function, which supports REGEX patterns for more complex matching.

*[matches(local-name(),'^OP')]
like image 66
Mads Hansen Avatar answered Sep 20 '22 12:09

Mads Hansen