Xidel:如何从许多相同的 value/class 中只选择 1 个并从结果中删除不需要的元素?

Xidel: How to choose only 1 from many same value/class and remove unneeded elements from the result?

xidel -se '//strong[@class="n-heading"][1]/text()[1]' 'https://www.anekalogam.co.id/id'

将打印出 3 个相同的输出

15 June 2020 
                     
15 June 2020 
                     
15 June 2020  

所以,我应该怎么做才能只选择其中的一个?

编辑:

强class里面的值是这样的:

 15 June 2020 
                     

如何只打印“2020 年 6 月 15 日”?

让我用下面的例子来说明为什么会这样。

'test.htm':

<html>
  <body>
    <div>
      <span>test1</span>
      <span>test2</span>
      <span>test3</span>
    </div>
    <div>
      <span>test4</span>
    </div>
    <div>
      <span>test5</span>
    </div>
    <div>
      <span>test6</span>
    </div>
  </body>
</html>
xidel -s test.htm -e '//div[1]/span[1]'
test1

xidel -s test.htm -e '//span[1]'
test1
test4
test5
test6

xidel -s test.htm -e '(//span)[1]'
test1

换句话说,您必须将 "strong"-节点放在括号之间:

xidel -s https://www.anekalogam.co.id/id -e '(//strong[@class="n-heading"])[1]/text()[1]'

如果您改为获取父节点,则不需要这样做:

xidel -s https://www.anekalogam.co.id/id -e '//p[@class="n-smaller ngc-intro"]/strong/text()[1]'

[奖金]

您可能已经注意到所需的文本节点跨越 2 行并以 &nbsp; 结尾。要 xidel return 只是“2020 年 6 月 15 日”:

xidel -s https://www.anekalogam.co.id/id -e '//p[@class="n-smaller ngc-intro"]/strong/normalize-space(substring-before(text(),x:cps(160)))'

- x:cps() is a shorthand for codepoints-to-string() (and string-to-codepoints()) 和 160 是 "No-Break Space".
的代码点 - 不需要 text()[1],因为每当您将序列提供给需要字符串的过滤器时,只会使用该序列的第一项。