XPATH: Check if a xml node contains specific characters or strings

女生的网名这么多〃 提交于 2019-12-11 06:48:37

问题


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;


来源:https://stackoverflow.com/questions/5635557/xpath-check-if-a-xml-node-contains-specific-characters-or-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!