Xpath for html id selection with random number

html html-agility-pack xpath

Question

Hello, how can I choose all links with 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 forth

Please take notice that the last component, "link3," is crucial and must be included in the Xpath.

I'm using HTML Agility Pack and C#.

1
1
11/10/2010 8:39:24 AM

Accepted Answer

If you use xpath 2.0, you may utilize regular expressions and the match/matches functions. If you're using xpath 1.0, you'll probably need to create your own custom attribute parser (take a look at xsl:function). According to my research, only xpath 2.0 supports the match function.

Most likely, @id[starts-with(., "List ct") and ends-with(., "link3")] is an alternative method.

2
11/10/2010 8:55:15 AM

Popular Answer

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

XPath expression to use:

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

Note: Nothing exists.fn:ends-with() inside XPath 1.0. Alternately, use last predicate.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow