XPATH - 将连接添加到多个属性中

时间:2013-12-13 10:39:46

标签: xpath

这是我的代码:

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://opengraphprotocol.org/schema/">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <link rel="alternate" hreflang="en" href="http://www.example.com/page-59.html"/>

<div id="" class="pgLinks">
<a href="/example-text" class="guiArw sprite-pagePrev ">&laquo;</a>
<a href="/example-text-2" class="paging taLnk ">1</a>
<span class="paging pageDisplay">2</span>

当我运行此查询时,它会返回页面“http://www.example.com/page-59.html”上的顶部网址,或者如果此处存在“1”,则返回

<a href="/example-text-2" class="paging taLnk ">1</a>

它从href返回URL:

/example-text-2

问题是我想要完整的网址:

http://www.example.com/example-text-2

我基本上需要在第二部分添加一个URL,以便它加入第二个结果(如果存在),所以它是这样的:

(//link[@hreflang='en'] |  "SITE URL HERE" //div[@class='pgLinks']/a[.='1'])[last()]/@href

我尝试过concat:

(//link[@hreflang='en'] | concat("http://www.example.com", //div[@class='pgLinks']/a[.='1']))[last()]/@href)

还有很多其他变种,包括使用管道“|”但根本无法理解。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

假设您只支持xpath 1.0,则可以执行xpath:

concat(
  substring(
    concat(
      'http://www.example.com',
      //div[@class='pgLinks']/a[.='1']/@href
    ),
    1 div boolean(//div[@class='pgLinks']/a[.='1'])
  ),
  substring(
    //link[@hreflang='en']/@href,
    1 div not(//div[@class='pgLinks']/a[.='1'])
  )
)

这是answer on implementing an if-else statement的应用程序。

相关问题