【问题标题】:Count specific punctuation in a tag using Xpath使用 Xpath 计算标签中的特定标点符号
【发布时间】:2020-04-11 07:56:35
【问题描述】:

我正在尝试计算标签“报告”中冒号后跟空格的数量:

<report>But that doesn't work either: what do you guys think: To be solved</report>

我的 xpath 代码

`report[count([text()=": "]) > 1]`

但它不起作用

【问题讨论】:

  • count(tokenize(/report/text(), ": "))-1
  • 我的实际 XML 文件标签是 <authors><author><gnm>Lee<gnm><snm>Auch<snm></author><report>Search Results: Count the number of words in a xml node using xsl: Thank you<report></authors> 我将您的代码调整为 /report[count(tokenize(/text(), ': '))-1 gt 1] 因为我必须在报告标签中找到多个冒号空间使用情况.. 但它不起作用

标签: xpath count punctuation


【解决方案1】:

要完成@Slkrasnodar 提供的 XPath 2.0 解决方案,这里是 XPath 1.0 解决方案:

string-length(//report)-string-length(translate(//report,":",""))

我们检查原始字符串的长度。然后我们生成一个没有冒号的字符串并检查它的长度。最后,我们从原始长度字符串中减去这个长度得到结果。

输出:2

编辑:请修复您的 XML 文件。它包含未封闭的标签。我合作过:

<authors>
<author>
<gnm>Lee</gnm>
<snm>Auch</snm>
</author>
<report>Search Results: Count the number of words in a xml node using xsl: Thank you</report>
</authors>

要获取报告中包含多个冒号的作者姓名,请使用以下表达式:

//authors[string-length(//report)-string-length(translate(//report,":",""))>1]//gnm/text()

输出:李

【讨论】:

  • 我的实际 XML 文件标签是 &lt;authors&gt;&lt;author&gt;&lt;gnm&gt;Lee&lt;gnm&gt;&lt;snm&gt;Auch&lt;snm&gt;&lt;/author&gt;&lt;report&gt;Search Results: Count the number of words in a xml node using xsl: Thank you&lt;report&gt;&lt;/authors&gt; 我将您的代码调整为 /authors[count(string-length(//report)-string-length(translate(//report,": ",""))) gt 1] 因为我必须在报告标签中找到多个冒号空间使用情况.. 但它不起作用
  • 好的。我已经发布了一个解决方案。请注意,您的 xml 无效。
  • 抱歉现在被XML修改了,&lt;authors&gt;&lt;author&gt;&lt;gnm&gt;Lee&lt;/gnm&gt;&lt;snm&gt;Auch&lt;/snm&gt;&lt;/author&gt;&lt;report&gt;Search Results: Count the number of words in a xml node using xsl: Thank you&lt;/report&gt;&lt;/authors&gt;你能帮帮我吗
  • 我已经做到了。您没有阅读我编辑的答案吗? stackoverflow.com/a/61155195/9978746
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
相关资源
最近更新 更多