0

I want to find all the elements which there attributes start with x.

For example,

<a xt="1"> text1 </a>
<a xu="2"> text2 </a>
<a text1="3"> text3 </a>

The xpath would find the first two elements, because it contains attributes with it name is xt and xu, respectively. text1 attribute doesn't start with x, and for that I will not get it.

I try with start-with() function, but as I understood it find the values, and not attributes.

2
  • Is it Xpath 1.0 or 2.0? In 2.0 you can use regular expressions. Commented Feb 6, 2014 at 8:04
  • I use WebDriver, find by xpath. Commented Feb 6, 2014 at 8:04

2 Answers 2

2

try

@*[starts-with(name(), 'x')]
Sign up to request clarification or add additional context in comments.

Comments

0

I would use:

//*[@*[starts-with(local-name(),'x')]]

Similar to the other answer, but uses local-name() instead. This will only check the attribute name and not the namespace prefix.

For example, if you use local-name() the following will also be matched because the prefix starts with x...

<a xns:text1="3" xmlns:xns="xns"> text3 </a>

If you use local-name(), it will not match because the local name of xns:text1 is text1.

3 Comments

i agree, but the OP did not mention any namespace.
@JoelM.Lamsen - The OP didn't mention namespaces, but with a question this basic I doubt the OP realizes the difference. I was just pointing out the differences so there wasn't any confusion in real world usage.
Yup, as I said, I agree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.