Xpath for selecting html id including random number

[亡魂溺海] 提交于 2019-12-13 02:34:21

问题


Hi how would I select all link when they have the following id.

<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">

And so on...

Please note that the last part "link3" is important, and must be included in the Xpath.

I'm using C# and Html agility pack.


回答1:


In case you use xpath 2.0 you can try match/matches functions and use regular expressions. If you are with xpath 1.0 probably you will have to write your custom attribute parser (take a look at xsl:function). AFAIR the match function is available only xpath 2.0.

Probably @id[starts-with(., 'List_ct') and ends-with(., 'link3')] is another way to do it.




回答2:


Hi how would I select all link when they have the following id

Use this XPath expression:

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

Note: There is no fn:ends-with() in XPath 1.0. Use last predicate instead.




回答3:


Use:

//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'),'_'))
       ]
   ]

This XPath expression selects all a elements in the document whose id attribute has string value with all of the following properties:

  1. Starts with the string 'List_ctl' .

  2. Ends with the string '_link3' .

  3. The substring surrounded by 'List_ctl' and '_' is a representation of an integer.



来源:https://stackoverflow.com/questions/4142575/xpath-for-selecting-html-id-including-random-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!