【问题标题】:how can i use regex in xpath 1.0我如何在 xpath 1.0 中使用正则表达式
【发布时间】:2018-10-25 11:04:18
【问题描述】:

这是我的 xpath

//dns:tbody//dns:td[1][@rowspan="1"]/text()

我想找到一个解决方案,如何让属性 rowspan @rowspan=" only number " 只接受数字

我正在寻找 XPath 1.0 中的正则表达式解决方案

正则表达式 =^[0-9]*$ 我的问题我不知道如何将正则表达式与 XPath1.0 或任何其他解决方案集成

【问题讨论】:

  • 使用 XPath 1.0 你不能,尽管使用正则表达式对于 XPath 2.0 来说是微不足道的。您可以使用number() 解决它
  • 我的 xpath 会像 //dns:tbody//dns:td[1][@rowspan=number()]/text()??

标签: regex xpath


【解决方案1】:

一些 XPath 实现允许您提供自己的函数。您需要查看有关 XPath 实现的文档。

Java:http://xml.apache.org/xalan-j/xpath_apis.html#functionresolver

【讨论】:

  • 我不允许只使用 XPath 函数,因为我使用的是工作流节点,只接受 XPath 查询
【解决方案2】:

尝试使用以下解决方案而不是正则表达式:

//dns:tbody//dns:td[1][number(@rowspan)<=0 or number(@rowspan)>0]/text()

这应该匹配 @rowspan 和 int 值

【讨论】:

  • n 号你能做到吗?
  • "only number" 你的意思是任何整数?负整数,零?有吗?
  • @MokiNex ,[number(@rowspan)&lt;=0 or number(@rowspan)&gt;0] 怎么样?
【解决方案3】:

您可以使用测试的解决方法:

  • 如果文本是数字(使用number() 函数)
  • 如果文本不是以- 开头(带有starts-with())
  • 如果文本不包含点(带有contains())

这样:

//dns:tbody//dns:td[1][string(number(@rowspan))!='NaN'][not(starts-with(@rowspan, '-'))][not(contains(@rowspan, '.'))]/text()

或

//dns:tbody//dns:td[1][not(string(number(@rowspan))='NaN' or starts-with(@rowspan, '-') or contains(@rowspan, '.'))]/text()

【讨论】:

  • 我需要在属性rowspan里申请
猜你喜欢
  • 2014-03-13
  • 1970-01-01
  • 2010-09-29
  • 2011-02-14
  • 2018-07-14
  • 1970-01-01
  • 2020-11-21
  • 2011-09-18
  • 1970-01-01
相关资源
最近更新 更多