XPATH: Check if a xml node contains specific chara

2019-08-12 06:34发布

问题:

Imagine this XML file (sorry for sick example!!)

<DOC>
<Test>
    <Name>1 vs 100 Pre pre History</Name>
    <Type>Dinasors</Type>
</Test>

<Test>
    <Name> 1 vs 100 Post moderns</Name>
    <Type>Aliens</Type>
</Test>
</Doc>

The idea is returning the Type value, By checking if Name node contains a specific character or string. For example I want a XPath Query like:

/Doc//Test[contains "Pre" and "History" and 1]/Type

That is check if Test contains "Pre" and "History" and number 1, What will be the xpath query? Alo I like this query to not be CASE SENSITIVE.

Thanks.

回答1:

I think this should work (haven't tested it):

/Doc//Test[contains(Name,'Pre') and contains(Name,'History') and contains(Name,1)]/Type

However that is case sensitive. If you want it case insensitive, then you may want to have a look at this question or this question: you must use translate to convert your Name to lower-case (or upper-case) and then match.



回答2:

it works when:

/Doc//Test[contains(Name,'Pre')][contains(Name,'History')][contains(Name,1)]/Type;