【问题标题】:How to apply substring-after and substring-before functions如何应用 substring-after 和 substring-before 函数
【发布时间】:2013-03-29 13:09:09
【问题描述】:

我的html结构如下

<td class="opps">1:2</td>

我正在尝试使用 XPath 拆分 td 的值,如下所示

.//*[contains(@class, 'opps')]/text()[normalize-space(substring-after(., ':'))]
.//*[contains(@class, 'opps')]/text()[normalize-space(substring-before(., ':'))]

但价值返回 1:2

如何分别提取12

我也在使用 HtmlNodeNavigator 和 HtmlAgilityPack。

怎么了?

提前谢谢你。

【问题讨论】:

  • 愚蠢的问题,但是如果你能得到1:2的值,为什么不把它解析出来后用C#拆分呢?
  • SO 上查看这个答案,有人用提供的解决方案提出了类似的问题stackoverflow.com/questions/3964642/…
  • @Oded 因为我想用 XPath 提取价值。
  • 没有真正回答我的问题。 “因为我想”。您是否有任何特定的技术问题意味着您必须在 XPath 中完成?
  • 它适用于使用 XPATH,除非我正在错误地阅读他想要的内容..

标签: c# xpath html-agility-pack


【解决方案1】:

经过更多搜索,我找到了解决方案。

如果使用 HtmlNodeNavigatorXPathNavigator,可能的解决方案如下

string _result1 = <HtmlNodeNavigator>.Evaluate("substring-after(.//*[contains(@class, 'opps')]/text(), ':')") as string; 
string _result2 = <XPathNavigator>.Evaluate("substring-before(.//*[contains(@class, 'opps')]/text(), ':')") as string;

【讨论】:

  • 感谢您找到自己的答案。我已经删除了我的。
  • 好吧,我说这是不可能的(因为我只考虑敏捷包和HtmlAgilityPack.HtmlDocument),所以这是公平的。
【解决方案2】:

以防万一,您也可以使用 c# 方法。 例如;

string strVal = "1:2";
string[] splitedStrArr = strVal.Split(':'); 
// you have now 2 elements inside the array. You can use it separately

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多