使用 Xpath 的 2 词链接

2-word links using Xpath

我需要在包含两个链接的页面上查找链接words.How这可以用 Xpath 完成吗?

<div class="navbar">
  <p>
    <a href="/navigate/names/a">Aaa aaa</a>
    <a href="/navigate/names/b">Bbb</a>
    <a href="/navigate/names/c">Ccc ccc</a>
    <a href="/navigate/names/d">Ddd</a>
    <a href="/navigate/names/e">Eee</a>
    <a href="/navigate/names/f">Fff fff ff</a>
  </p>
</div>

如果可以通过空格数区分字符串,则可以使用此 XPath-1.0 表达式:

/div/p/a[string-length(normalize-space(.))-string-length(translate(normalize-space(.),' ',''))=1]

这匹配所有 two-word-strings.

我的 2cts 通过删除不 space 个字符并计算 spaces

XPath 1: //a[count(translate(normalize-space(.),"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ","")) = 1]

echo -e 'cat //a[translate(normalize-space(.),"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ","") = " "] \n bye' | xmllint --shell test.html
/ > cat //a[translate(normalize-space(.),"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ","") = " "] 
 -------
<a href="/navigate/names/a">Aaa  aaa</a>
 -------
<a href="/navigate/names/c">Ccc ccc</a>
/ >  bye

使用剩余 spaces 的长度

Xpath 2://a[string-length(translate(normalize-space(.),"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ","")) = 1]

echo -e 'cat //a[string-length(translate(normalize-space(.),"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ","")) = 1] \n bye' | xmllint --shell test.html
/ > cat //a[string-length(translate(normalize-space(.),"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ","")) = 1] 
 -------
<a href="/navigate/names/a">Aaa  aaa</a>
 -------
<a href="/navigate/names/c">Ccc ccc</a>
/ >  bye