2010-11-10 97 views
1

嗨如何在選擇具有以下標識的所有鏈接時選擇所有鏈接。用於選擇包含隨機數的html標識的Xpath

<a id="List_ctl01_link3" class="content" href=link1.aspx"> 
<a id="List_ctl02_link3" class="content" href=link2.aspx"> 
<a id="List_ctl03_link3" class="content" href=link3.aspx"> 
<a id="List_ctl04_link3" class="content" href=link4.aspx"> 

等等......

請注意,最後一部分「LINK3」是很重要的,並且必須包含在XPath。

我使用C#和Html敏捷包。

+0

好問題,+1。看到我的答案迄今爲止最精確的解決方案。 :) – 2010-11-11 05:55:50

回答

2

如果您使用xpath 2.0,您可以嘗試使用match/matches函數並使用正則表達式。如果你使用xpath 1.0,你可能需要編寫自定義屬性解析器(看看xsl:function)。 AFAIR匹配功能僅適用於xpath 2.0。

可能是@id [開頭(。,'List_ct')和結束 - (。,'link3')]是另一種方式。

+0

Thx,我使用了開始與解決方案,它的工作原理:-) – gulbaek 2010-11-10 09:56:38

+0

請注意XPath 1.0中沒有'fn:ends-with()' – 2010-11-10 15:11:47

+0

@Ajjandro,謝謝。我假設如果在xpath 1.0中有fn:starts-with(),出於一致性原因,應該有fn:ends-with()。我不好。必須檢查。 – 2010-11-11 05:58:51

5

您好我怎麼會選擇所有鏈接 時,它們具有以下ID

使用該XPath表達式:

//a[@id[starts-with(.,'List_ctl')][substring(.,string-length()-5)='_link3']] 

注意:有XPath中沒有fn:ends-with() 1.0。改爲使用最後一個謂詞。

+0

+1爲更好的解決方案。 – 2010-11-11 03:41:23

3

使用

//a[@id[starts-with(.,'List_ctl') 
     and 
     substring(.,string-length()-5)='_link3' 
     and 
     floor(substring-before(substring_after(.,'List_ctl'),'_')) 
     = 
     floor(substring-before(substring_after(.,'List_ctl'),'_')) 
     ] 
    ] 

此XPath表達式選擇id屬性具有與所有的以下性質中的字符串值的文檔中的所有a元素:

  1. 開始與字符串'List_ctl'

  2. 結束字符串'_link3'

  3. 'List_ctl''_'包圍的子串是整數的表示。

+0

+1爲最精確的一個:-) – 2010-11-11 05:54:53

+0

Ja! +1精確度! – 2010-11-11 13:12:40