Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:31:00+00:00 2026-06-04T16:31:00+00:00

$list = array(‘br’, ‘hr’, ‘link’, ‘meta’, ‘title’); Using DOMXpath, how can I select nodes

  • 0

$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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:31:01+00:00Added an answer on June 4, 2026 at 4:31 pm

    You didn’t give us any XML to work with, which is not very nice, but here you go:

    $xml = <<<XML
    <div>
       <a>
       </a>
       <p>some text</p>
       <p></p>
       <span>no text
          <hr/>
          <ul></ul>
       </span>
       <br/>
    </div>
    XML;
    
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    $xpath = new DOMXPath($dom);
    $list = array('br', 'hr', 'link', 'meta', 'title');
    $expr = array();
    foreach ($list as $l) {
       $expr[] = "not(self::$l)";
    }
    $expr = implode(' and ', $expr);
    
    foreach ($xpath->query("//*[$expr and not(normalize-space())]") as $elem) {
       echo "$elem->nodeName\n";
    }
    

    This outputs

    a
    p
    ul
    

    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 the nodeName is 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 use node() instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I return a list/array of all keys that have an error? I
For the new computer science assignment we are to implement a list/array using an
I have an array list of objects and I am using this example to
I have an Array List that I want to output like my example below.
can we search string in another string/list/array in django which is passed through python
i am using spinner in some application in spinner item list array this text
I get a string from a array list: array.get(0).toString() gives TITLE = blabla I
I'm trying to write an array (list?) to a text file using Python 3.
I need to have a list(array, ect) of objects that have a static values.
I am trying to gather a list (array) of ids in a sector <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.