Reputation: 904
PART A
I'd like to select an HTML element by its attribute values. I'd like to be able to match all the attributes.
So an element <a>
with the following attributes for example:
'href' => "https://twitter.com/JSTOR"
'target' => "_blank"
'class' => "icon-twitter twitter-styling icon-large"
'aria-label' => "Twitter - This link opens in a new window"
Is it like this: //a[@href='https://twitter.com/JSTOR' AND @target='_blank' AND @class='icon-twitter twitter-styling icon-large' ...
So if I have n attributes, I need n-1 "AND"?
PART B
Also worth noting is the multiple values in "class". Can I simply have @class='classA classB classC' or should I @class='classA' AND @class='classB' AND @class='classC'?
PART C
Lastly, for the multiple values in "class", does the order matter? If the tag lists 'classA classB' and we use an xpath //a[@class='classB classA', will it still match?
**Please note I need an answer for XPath 1.0
Upvotes: 1
Views: 106
Reputation: 111491
This XPath illustrates all of the concepts of your three-part question:
//a[contains(concat(' ',@class,' '), ' classA ')]
[contains(concat(' ',@class,' '), ' classB ')]
[contains(concat(' ',@class,' '), ' classC ')]
[@href='https://twitter.com/JSTOR']
[@target='_blank']
Notes:
@class
tests prevent classA
from matching classAnt
.and
to join your conditions in a single predicate rather than compound predicates.Upvotes: 1
Reputation: 2632
So if I have n attributes, I need n-1 "AND"?
Yes.
Can I simply have
@class='classA classB classC'
You can, but it will require exact match, including whitespace and order
Lastly, for the multiple values in "class", does the order matter?
It does matter if you do equality match. There are ways around this, see this answer
Upvotes: 1