开发者

xPath: Select parent element only if child element doesn't contain a certain value

Given this:

<element_1> 
    <element_2>Text</element_2> 
    <element_3> 
        <element_4> 
            <element_5>Text with @ in Value</element_5> 
        </element_4> 
        <element_4> 
            <element_5>Test Text</element_5> 
        </element_4> 
    </element_3> 
    <element_6> 
        <element_7> 
            <element_8>0</element_8> 
...

How do I select all instanc开发者_StackOverflow中文版es of element_1 that do not contain an '@' in element_5?


element_1[not(contains(element_3/element_4/element_5,'@'))]

This is interpreted as

element_1[                          #select elements named "element_1"
  not(contains(                     #that do not contain
    element_3/element_4/element_5,  #children in this hierarchy
    '@'                             #the character @
  ))
]


My XPath is a bit rusty, but something like this:

//element_1[.//element_5[not(contains(., "@"))]]


How do I select all instances of element_1 that do not contain an '@' in element_5?

Use:

//element_1[not(.//element_5[contains(.,'@')])]

In English, this means: Select all "element_1" elements in the document that do not have an "element_5" descendant whose string value contains the string '@'.

I am using the // abbreviation only because the structure of the XML file isn't clear.

I always recommend to avoid using the // abbreviation always when this is possible, because otherwise this may result in very inefficient XPath evaluation.


element1[not(contains(.//element_5, '@'))]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜