XPath的选择的innerText [英] XPath select innertext

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

问题描述

我有这样的HTML / XML:

  \t\t\t\t\t \\ \\r\\\
\t\t
< A HREF =/ Test.aspx文件>
<跨度类=试验>
< B>&布拉布拉LT; / B>
< / SPAN>
< / A>
< BR />
这就是我想要
将文本; BR />
<跨度类=测试>
< B>代码:123 LT; / B>
< / SPAN>
< BR />
<跨度类=测试>< / SPAN>
\t\t\t\t\t\t\t\t\t\t\t\t\r\\\
\t\\ \\t\t

在C#4我用HtmlAgilityPack LIB选择使用XPath节点和获得InnerText属性。这将让该节点内的所有文本。我怎样才能得到只有文字:这是我想要的文字?



/文()返回 \t\t\t\t\t \r\\\
\t\t

解决方案

  / DIV /文本()

从给出的例子,这个XPath将让你的div元素下的所有文本节点,在这种情况下test2的。



如果你能更多地讨论这个问题,我们也许能更好地帮助你。该div包含3个孩子:一个span元素,文本节点和B元素。跨度和b每一个具有文本子节点。使用XPath你只能选择元素(/ DIV / *),文本节点只(/ DIV /文本())或所有节点类型(/ DIV /节点())。



编辑:/文本()将只返回您根级文本节点。在这种情况下,我希望它返回一个包含3文本节点一个节点列表:

  \t\t\t \t\t \r\\\
\t\t
这就是我想要
\t\t\t\t\t\t文\t\t\t\t\t\t\r\\\
\t\t\t

您或许只是选择在生成的节点列表中的第一个节点?
有良好格式的一些问题,比如你的< BR> 也许应该是< BR />


I have this HTML/XML:

\t\t\t\t\t    \r\n\t\t
<a href="/test.aspx">
  <span class=test>
    <b>blabla</b>
  </span>
</a>
<br/>
this is the text I want
<br/>
<span class="test">
  <b>code: 123</b>
</span>
<br/>
<span class="test"></span>
\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t

In C#4 I use the HtmlAgilityPack lib to select the Node with XPath and get the InnerText property. This will get all the text inside the node. How can I get only the text "this is the text I want"?

/text() only returns \t\t\t\t\t \r\n\t\t

解决方案

/div/text()

From the example given, this XPath will get you all text nodes underneath the div element, in this case test2.

If you could elaborate more on the question we might better be able to help you. The Div contains 3 children: a span element, a text node and a b element. The span and b each have a text node child. Using XPath you could select elements only (/div/*), text nodes only (/div/text()) or all node types (/div/node()).

EDIT: /text() will only return you root level text nodes. In this case I would expect it to return a node list containing 3 text nodes:

\t\t\t\t\t    \r\n\t\t 
this is the text I want
\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t

Are you perhaps only selecting the first node in the resultant node list? There are a few issues of well-formedness such as your <br> should probably be <br/>.

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

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