text() XPath 选择器中的重复选择 [英] duplicate selection in text() XPath selector

查看:21
本文介绍了text() XPath 选择器中的重复选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择文本中包含某个单词(例如:公司)的所有节点,因为该单词需要有一个注册标记.

I'm trying to select all nodes with text that contain a certain word (ex: Company) because the word needs to have a register mark.

这是 XHTML 的一部分(这个

在表格单元格内).

Here is part of the XHTML (this <p> is inside a table cell).

<p>
  <strong>
    <a style="color:#0E5A8B; text-decoration:none" target="_blank" href="http://www.trekk.com">
      <span class="title">
        A Company Content Title
      </span>
    </a>
    <br />
    <span style="color:#000000">
      February 23, 2010 10:00 A.M. PT<br />
    </span>
  </strong>
  Sample Content<br />
  <a style="color:#000" target="_blank" href="http://www.trekk.com">
    Register now
  </a>
</p>

我将 XHTML 加载到 System.Xml.XmlDocument 并尝试使用

I load the XHTML into a System.Xml.XmlDocument and try to select the nodes using

NewsletterHtmlDoc.SelectNodes("//*[contains(text(),'Company')]")

生成的 XmlNodeList 包含 2 个 XmlNode.

The resulting XmlNodeList contains 2 XmlNodes.

  1. with InnerText = 公司内容标题 2010 年 2 月 23 日上午 10:00PT 样本内容立即注册

  1. <p> with InnerText = A Company Content Title February 23, 2010 10:00 A.M. PT Sample Content Register now

with InnerText = 公司内容标题

<span class="title"> with InnerText = A Company Content Title

我的目标是只选择第二个,<span> 标签,我不确定为什么 <p> 标签也被选中.如果它选择了

,但为什么它不也选择 ,以及为什么不是包含

?

My goal is to just select the 2nd one, the <span> tag and am not sure why <p> tag is also being selected. If it selects <p>, but why wouldn't it also select the <strong> or <a>, and why not the <table> or <td> that contain the <p>?

推荐答案

我想通了.Sample Content 文本是

元素的 InnerText 的一部分,并导致

元素的整个 InnerText被发现.将 Sample Content 放在它自己的元素中,从

元素中删除了任何直接的 InnerText 并解决了问题.

I figured it out. The Sample Content text was part of the InnerText of the <p> element and caused the entire InnerText of the <p> element to be found. Putting Sample Content in its own element removed any immediate InnerText from the <p> element and fixed the problem.

<p>
  <strong>
    <a style="color:#0E5A8B; text-decoration:none" target="_blank" href="http://www.trekk.com">
      <span class="title">
        A Company Content Title
      </span>
    </a>
    <br />
    <span style="color:#000000">
      February 23, 2010 10:00 A.M. PT<br />
    </span>
  </strong>
  <span>
    Sample Content
  </span>
  <br />
  <a style="color:#000" target="_blank" href="http://www.trekk.com">
    Register now
  </a>
</p>

这篇关于text() XPath 选择器中的重复选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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