如何使用XPath从所选文本中筛选某些单词?

如何使用XPath从所选文本中筛选某些单词?,xpath,Xpath,要在此处选择文本,请执行以下操作: Alpha Bravo Charlie Delta Echo Foxtrot 从这个HTML结构: <div id="entry-2" class="item-asset asset hentry"> <div class="asset-header"> <h2 class="asset-name entry-title"> <a rel="bookmark" href="http:

要在此处选择文本,请执行以下操作:

     Alpha Bravo Charlie Delta Echo Foxtrot
从这个HTML结构:

<div id="entry-2" class="item-asset asset hentry">
  <div class="asset-header">
    <h2 class="asset-name entry-title">
      <a rel="bookmark" href="http://blahblah.com/politics-democrat">Pelosi Q&amp;A</a>
    </h2>
  </div>
  <div class="asset-content entry-content">
    <div class="asset-body">
     <p>Alpha Bravo Charlie Delta Echo Foxtrot</p>
    </div>
  </div>
</div>

我如何清理文本中的以下词语:

Alpha 
Charlie 
Echo 
因此,在本例中,我只得到以下文本:

Bravo Delta 

我如何清理文本中的以下词语:

Alpha 
Charlie 
Echo 
因此,在本例中,我只得到以下文本:

Bravo Delta 
仅在XPath 1.0中无法做到这一点-您需要获取宿主语言中的文本并在那里进行替换

在XPath 2.0中,可以使用replace函数:


对于XPath 1.0,假设使用uniques NMToken:

concat(substring-before(concat(' ',$Node,' '),' Alpha '),
       substring-after(concat(' ',$Node,' '),' Alpha '))
正如您所看到的,这会变得非常冗长和糟糕

使用XPath 2.0:

string-join(tokenize($Node,' ')[not(.=('Alpha','Charlie','Echo'))],' ')

好问题,+1。有关解释和XPath2.0解决方案,请参见我的答案。
concat(substring-before(concat(' ',$Node,' '),' Alpha '),
       substring-after(concat(' ',$Node,' '),' Alpha '))
string-join(tokenize($Node,' ')[not(.=('Alpha','Charlie','Echo'))],' ')