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

  • Home
  • SEARCH
  • 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 8237145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:22:39+00:00 2026-06-07T19:22:39+00:00

How can I get the following information in xpath? Text 01 – link_1.com Text

  • 0

How can I get the following information in xpath?

Text 01 – link_1.com

Text 02 – link_2.com

$page = '
<div class="news">
<div class="content">
    <div>
    <span class="title">Text 01</span>
    <span class="link">link_1.com</span>
    </div>
</div>
<div class="content">
    <div>
    <span class="title">Text 02</span>
    <span class="link">link_2.com</span>
    </div>
</div>
</div>';
@$this->dom->loadHTML($page);
$xpath = new DOMXPath($this->dom);

// perform step #1
$childElements = $xpath->query("//*[@class='content']");
$lista = '';
foreach ($childElements as $child) {
    // perform step #2
    $textChildren = $xpath->query("//*[@class='title']", $child);
    foreach ($textChildren as $n) {
        echo $n->nodeValue.'<br>';
    }

    $linkChildren = $xpath->query("//*[@class='link']", $child);
    foreach ($linkChildren as $n) {
        echo $n->nodeValue.'<br>';
    }
}

My result is returning

Text 01

Text 02

link_1.com

link_2.com

Text 01

Text 02

link_1.com

link_2.com

  • 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-07T19:22:40+00:00Added an answer on June 7, 2026 at 7:22 pm

    Replace // by descendant:: in second and third xpath, because // tells xpath to search this element evrywhere in xml and not in specific node (as you need), and $child is NOT separate XML. descendat:: means any child node

    @$this->dom->loadHTML($page);
    $xpath = new DOMXPath($this->dom);
    
    // perform step #1
    $childElements = $xpath->query("//*[@class='content']");
    $lista = '';
    foreach ($childElements as $child) {
        // perform step #2
        $textChildren = $xpath->query("descendant::*[@class='title']", $child);
        foreach ($textChildren as $n) {
            echo $n->nodeValue.'<br>';
        }
    
        $linkChildren = $xpath->query("descendant::*[@class='link']", $child);
        foreach ($linkChildren as $n) {
            echo $n->nodeValue.'<br>';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From windows event viewer I can get the following xml structure: <Event xmlns=http://schemas.microsoft.com/win/2004/08/events/event> <System>
Using the unix dig command, I can get the following MX record: $ dig
I can't get the following properties to work :/ mail mailNickname name cn dn
How can I get the following test to pass with NHibernate? I thought it
This is basic stuff but I can't get this following form to process reliably.
I'm new to Javascript and for some reason I can't get the following code
I tried following the instruction s but can't get the plugin to work, the
I get the following errors in DDMS . But how can I trace this
What can you do if you get the following error when installing Visual Studio
How can I get accurate window information in Linux? I know that I can

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.