简体   繁体   中英

XPath and PHP DomDocument - How to select all childs with specific tag and attribute

I have the element $paragraph in my DOMDocument:

<p class=4textlist>
    String 1
    <p class=4textlist> String 1.1</p>
<p class=4textlist>String 1.2</p>
    String 2
</p>

At one moment, I selected the element and I would like to get all the p tags from this elements that have the class "4textlist".

For that, I have try:

$result = $xpath->query('descendent::p[@class="4textlist"]', $paragraph);

But this is not working. Any idea please ?

You've got a typo in your query, descendent vs. descendant . descendant::p[@class="4textlist"] works fine.

You might also consider .//p[@class="4textlist"] .

尝试这个:

//p[@class="4textList"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM