You probably meant //article[.//h3[ text() = "eZ Publish - Man jacket"] ]//input
, that is with a relative XPath expression (starting with '.') in the article
context
Through XPath find an element that has a child with specific text
Question
I've seen similar questions on the wild, but for some reason I couldn't manage yet to find a correct way to do.
So I got this page
Under main items there are 2 articles (shop items)
- eZ Publish - Man jacket
- eZ Publish - iPhone 4 Case
I want to get the input (text) field in the article that contains the man jacket
What have I already tried:
//article[ //h3[ text() = "eZ Publish - Man jacket"] ]//input
//article[ //h3[contains(text(), "eZ Publish - Man jacket")] ]//input
//article[ contains( .//h3[ text() = 'eZ Publish - Man jacket' ] ) ]//input
The next one works:
//h3[text()='eZ Publish - Man jacket']/../../..//input
... but this isn't kind of an option...
Any idea what am I doing wrong?
La solution
Autres conseils
Your first XPath expression works, however according to your source XML you won't see any values as your "input" elements don't have values, rather they have attributes that have values.
If you want to output all the attribute values you would need to add /@* after "input" to specify all (*) the attributes (/@).
Your second XPath expression won't work as you are trying to pass a node-tree (text() which is being run against //h3 therefore returning multiple nodes) as a string argument to the contains function.
Your third XPath expression won't work as you are only passing one argument to contains, which requires two.
Hope this helps.