c11ada
c11ada

Reputation: 4334

XSL match node on multiple values

i have an xml document which looks abit like

<a>
<somenode att="1"></somenode>
</a>
<a01>
<apple att="2"></apple>
<somenode att="1"></somenode>
</a01>

what im trying to do is match when node name is 'a' or a followed by one number (a0) and a followed by two number (a01), any ideas on how I can do this ?

I have the following so far

<xsl:apply-templates select="node()[starts-with(name(),'a')]">

but this will select apple aswell, also how to do multiple matches like and OR/AND ?

Upvotes: 0

Views: 526

Answers (1)

JLRishe
JLRishe

Reputation: 101652

In XSLT 2.0 this would be pretty simple:

<xsl:apply-templates select="node()[matches(name(),'^a\d?\d?$')]">

In XSLT 1.0, regex isn't available, so it's a little trickier, but the following should work:

<xsl:apply-templates select="node()[starts-with(name(), 'a') and
                                    string-length(name()) &lt;= 3 and
                                    translate(name(), '0123456789', '') = 'a']" />

The three parts joined by "and" here ensure that:

  • The name starts with "a"
  • The name is 3 characters or less
  • All that remains when digits are removed from the name is the string "a" (meaning that any characters besides that single "a" were digits).

Upvotes: 4

Related Questions