1

My team is currently leveraging SonarQube to do code quality checks on our Mulesoft API implementations. SonarQube uses XPATH 1.0 to create custom rules on mule 4 code. The issue we are currently facing is that, in the event there is same named elements with same named attributes there is no explicit way loop over them and match their values with regex. For example:

<a>
  <b>
    <c d='123'></c>
  </b>
  <b>
    <c d='123'></c>
  </b>
  <b>
    <c d='123'></c>
  </b>
  <b>
    <c d='xyz'></c>
  </b>
</a>

The rule would be to determine if all attributes 'd' are using digits only. This rule could be done using XPATH 2.0 thus:

every $value in /a/b/c/@d satisfies matches($value, '^\d+$')

However as this is not valid in SonarQube is there a way in XPATH 1.0 that this rule can be achieved?

M_B
  • 11
  • 1

2 Answers2

0

No, XPATH 1.0 does not support regex.
See this or this discussion.
There are some workarounds that also mentioned here or in more other places.

Prophet
  • 32,350
  • 22
  • 54
  • 79
0

You can't do this with regular expressions in XPath 1.0.

But you can check if a string contains (ascii) digits only using

translate(@x, '0123456789', '') = ''

And you can do the universal quantification as:

not(/a/b/c[not(translate(@x, '0123456789', '') = '')])

Or by exploiting string-to-boolean conversion, as:

not(/a/b/c[translate(@x, '0123456789', '')])
Michael Kay
  • 156,231
  • 11
  • 92
  • 164