问题
I have the following structure:
<table>
...
<tr><td><input name="email" /></td></tr>
...
</table>
Question: what is the expression to select that tr based on the "name" attribute of the "input" tag? More specifically, I want to know if there is a way to do this without having to select the input and then go up the hierarchy doing ->parentNode->parentNode... Thanks in advance.
回答1:
Use:
//table/tr[td/input/@name = 'email']
This means:
Select all tr
elements that are children of a table
and that have a child td
that has a child input
with arrtibute name
whose value is the string 'email'
.
Do note: No reverse axis is used in the expression.
回答2:
Try:
//input[@name="mail"]/ancestor::tr[1]
Note:a XPATH->query doesn't return a specific element, it returns a nodeList(also if it's clear that there is only one item inside). You'll need do select the item using
nodeList->item(0);//returns the first item of the nodeList.
来源:https://stackoverflow.com/questions/4811949/php-domxpath-expression-to-select-a-tr-that-contains-an-input-with-certain-att