PHP DOMXPath - expression to select a tr that contains an input with certain attribute

匆匆过客 提交于 2019-12-23 02:34:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!