1

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

kjhughes
  • 106,133
  • 27
  • 181
  • 240
forgodsakehold
  • 870
  • 10
  • 26

2 Answers2

1

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

weirdan
  • 2,499
  • 23
  • 27
1

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:

  • The @class tests prevent classA from matching classAnt.
  • You can use and to join your conditions in a single predicate rather than compound predicates.
kjhughes
  • 106,133
  • 27
  • 181
  • 240