$list = array('br', 'hr', 'link', 'meta', 'title');
Using DOMXpath, how can I select nodes that are empty and their tagName is not within $list?
(I want to add a space in their textContent so they are not automatically closed)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You didn’t give us any XML to work with, which is not very nice, but here you go:
This outputs
As expected. Now you have the nodes — it’s up to you to add the space. IMO it would be easier to just use
not(normalize-space())and then see if thenodeNameis not in your list, but you asked for an XPath expression, so that’s what you got.Note that
normalize-space()is used because pure whitespace may still cause the node to automatically close. If that’s not an issue, you can usenode()instead.