use an extra pair of brackets
(//a[contains(@style, 'visible')]/div[contains(t
Main Topics
Browse All TopicsHI Experts
I have this xml file :
<sut>
<a>a1</a>
<a style="visible">
a2
<div>Add1</div>
</a>
<a>a3</a>
<a style="visible">
a4
<div>Add2</div>
</a>
<a style="visible">
a5
<div>Edit</div>
</a>
</sut>
if I have this xpath query :
//a[contains(@style, 'visible')]/div[contains(t
I get 2 answers :
<div>Add1</div>
<div>Add2</div>
I want to get only the last query : <div>Add2</div>
I tried to write
//a[contains(@style, 'visible')]/div[contains(t
or
//a[contains(@style, 'visible')]/div[contains(t
but it didn't worked.
does any one know what is the right xpath query to get the second answer ?
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Aha, late in the poarty again, I see :-)
The problem of this is the priority of the predicate.
The problem you were having was the following
The [2] belongs to /div[contains(text(), 'Add')], not to the entire Nodeset found in the full XPath
Brackets get around that, allthough I am not certain it works with all XSLT1.0 processors (abel?)
A quick test shows me it works in Xalan and Saxon6, I assume it is general.
It is definitely the easiest approach in XSLT2
Abel showed a nice way of forcing all in predicates instead of having a forward "/"
overcoming this problem as well.
Maybe his method is preferable, I don't know.
There is one risk hidden in that method, which does not show up here but will do if you approach the issue this way in a general context
If there would be more than one <div> in <a>, you would need to repeat the predicate on div,
a bit like this
/sut/a[@style = 'visible'][div[contains(te
I hope you understand why
Good luck
Geert
Let's follow up on a few of those clear observations and questions from Gertone:
Business Accounts
Answer for Membership
by: abelPosted on 2009-06-16 at 07:25:11ID: 24638339
You can try the following (the last element is "Edit", but I assume you mean the last qith content "Add"):
which will give you the <a> element. If you want the <div>, you can change that to:
and for performance reason, it is best to use absolute (or relative) XPaths, which in this case would be:
-- Abel --