问题
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:
Starts with the string
'List_ctl'
.Ends with the string
'_link3'
.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