Xpath for selecting html id including random number

html html-agility-pack xpath

Question

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.

Accepted Answer

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.


Popular Answer

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.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why