Contains function returns nodes with all the Substrings. How Can I Avoid that

Viewed 25

I am using './/*[contains(local-name(), "revenue")]' to retrieve all the nodes exactly matches "revenue" tag with all the variation(Upper case,lower case and Mixed) and not the nodes that has "revenue" as substring.

from the below xml data. I want to retrieve "revenue"and "Revenue" tags only and not "interestRevenue","transactionrevenue", "revenueOtherServices" tags

<financial_statement>
  <revenue>123</revenue>
  <Revenue>456</Revenue>
  <interestRevenue>234</interestRevenue>
  <salaries>12</salaries>
  <transactionrevenue>345</transactionrevenue>
  <revenueOtherServices>109</revenueOtherServices>
  <sales>783</sales>
  <costs>746</costs>
  .....
</financial_statement>

Is there any way to do it ?

Thanks in advance.

2 Answers

Like this ( 2.0):

/financial_statement/*[lower-case(name()) = "revenue"]

For xpath 1:

/financial_statement/*[translate(name(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") = "revenue"]

To complete @Gilles Quenot's answer, 2 alternatives :

Classic form :

//*[name()="Revenue" or name()="revenue"]

More complex :

//*[string-length(name())=7 and substring(name(),2,7)="evenue"]
Related