多条件XML文本提取的XPath规则需求(无代码实现)
符合需求的XPath单规则方案
最终单条XPath表达式
//P[ @countries='AR' or (not(@countries) and (ancestor::*[@countries='AR'] or not(ancestor::*[@countries]))) ]
表达式拆解(对应三类提取条件)
@countries='AR':直接匹配自身countries属性为“AR”的<P>元素,对应条件1not(@countries) and ancestor::*[@countries='AR']:匹配自身无countries属性,但存在祖先元素的countries属性为“AR”的<P>元素,对应条件2not(@countries) and not(ancestor::*[@countries]):匹配自身及所有祖先元素均无countries属性的<P>元素,对应条件3
简化紧凑写法(逻辑等价)
如果需要更简洁的表达式,可合并后两个条件的公共逻辑:
//P[@countries='AR' or not(@countries) and (ancestor::*[@countries='AR'] or not(ancestor::*[@countries]))]
内容的提问来源于stack exchange,提问作者Paul
相关产品推荐
相关产品推荐

