如何使用XPath选择器排除子节点? [英] How to exclude child nodes with XPath selector?

查看:573
本文介绍了如何使用XPath选择器排除子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML片段:

 < div class =d1> 
< p>
text
< br />
< div class =d2>文字< / div>
...
< a class =a1> a1< / a>
< a class =a2> a2< / a>
< / div>

我想要获取 d1 类,并且使用此类排除 a1 a2 类:

  // div [contains(@class,d1)and(@class!=a1)and(@class!=a2)] 

但是这个失败了。

我该如何编写选择器?

解决方案

XPath用于选择,而不是用于转换。 (XSLT用于转换。)



您可以选择 div 不包含某些 元素 仅当输入文档中已存在 div 元素 ;你不能选择 div 元素,然后删除一次选定的子元素。


I have the following HTML fragment:

<div class="d1">
 <p> 
   text
 <br/>
 <div class="d2"> text </div>
 ...
 <a class="a1">a1</a>  
 <a class="a2">a2</a>
</div>

I want to get the content of d1 class and also exclude a1 and a2 classes using this:

//div[contains(@class, "d1") and (@class!="a1") and (@class!="a2")]

but this failed.

How can I write the selector ?

解决方案

XPath is for selection, not for transformation. (XSLT is for transformation.)

You can select div elements that do not contain certain a elements only if the div elements already exist in the input document; you cannot select div elements and then remove child elements once selected.

这篇关于如何使用XPath选择器排除子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆