Reputation: 2043
Have many xmlfiles like this whose structure is not same and may vary.
<ratings>
<rating>
<agency>SP</agency>
<provider>SP</provider>
<type>LONG TERM</type>
<currencyType>LOCAL</currencyType>
<description>SP Standard LT LC rating</description>
<crating by = "Moodys">
<code>BBB+</code>
</crating>
<crating by = "S&P">
<code>AAA-</code>
</crating>
<date>2011-09-07</date>
</rating>
</ratings>
I would like to find all xml documents that have a text of BBB+ and AAA- Since the xml structures vary i have to resort to wild cards and cannot rely on element names any ideas how would i do this please.
My try is but it doesnt work
//*[. = "BBB+" and . = "AAA-"]
If i don't use the "and" it works but then it wouldn't meet my criteria.
Upvotes: 1
Views: 65
Reputation: 9644
EDIT: This actually answers the case where you want only one criteria met, ie there's at least one node with the text 'AAA-' or 'BBB+'
You can use
//*[normalize-space(text()) = 'AAA-' or normalize-space(text()) = 'BBB+']
which will return the node where the text is found. You can directly put a condition on the text()
attribute, and the normalize-space
function trims the text from trailing whitespaces.
This will obviously be quite slow as it requires to check all nodes. If you can change the *
into a code
, this would greatly increase performance.
Upvotes: 1
Reputation: 89285
You can try this way :
/*[//*[normalize-space(text()) = 'AAA-'] and //*[normalize-space(text()) = 'BBB+']]
That will return element if there are "text of BBB+ and AAA-", and return nothing/null otherwise. In addition to this, check answer from @Robin, it contains explanation and good advice to consider.
Upvotes: 1