lape
lape

Reputation: 41

Xpath filter to find specific value in nested blocks

I inherited a legacy system with an XPath filter which is not my strong side.

Example XML (which I cannot change):

<bill>
    <header>
       <no>1234</no>
    </header>
    <details>
       <company>C1009</company>
       <lineNo>1</lineNo>
       <amount>770.50</lineNo>
    </details>
    <details>
       <company>C1009</company>
       <lineNo>2</lineNo>
       <amount>1050.50</lineNo>
    </details>
</bill>

I need a function to find a specific company number for each bill-xml. In this case it is company 1009. The function just needs to return a true/false (empty or found). How to solve this?

There is already a filter for another purpose in the system which successfully finds a field but I cannot get it to work with the multiple lines:

/*[local-name() = 'details']/*[local-name()='company' and contains(text(),'C1009')]

Upvotes: 0

Views: 731

Answers (1)

Mark Veenstra
Mark Veenstra

Reputation: 4739

The next XPath will match all <details> elements with an element <company> which has a value that contains the text 1009:

/bill/details[contains(company, '1009')]

If you don't want to match on a part of the text, but you know the exact value in <company> element, you can use:

/bill/details[company = 'C1009']

Also note that local-name() can be used, but if there are no namespaces, you can use the XPath as I described. Otherwise you can use local-name() or declare the namespace and use that prefix.

Upvotes: 1

Related Questions