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 6819045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:15:06+00:00 2026-05-26T21:15:06+00:00

How can I get each node’s attribute via xpath? For instance, book.xml, <?xml version=1.0

  • 0

How can I get each node’s attribute via xpath?

For instance,

book.xml,

<?xml version="1.0" encoding="UTF-8" ?>
<records timestamp="1264777862">
<record></record>
<record></record>
<record timestamp="1264777000"></record>
<record></record>
</records>

php,

<?php

$doc = new DOMDocument;

$doc->load('book.xml');

$xpath = new DOMXPath($doc);

# get and output "<entry>" elements
$x = $doc -> getElementsByTagName('record');

# Count the total feed with xpath.
$total = $x->length;

# the query is relative to the records node
$query = 'string(/records/@timestamp)';

for ($i=0; $i<$total; $i++)
{
    $timestamp = $xpath->evaluate($query,$x->item($i));
    echo $timestamp ."<br/>";
}

?>

result (which it loops the first node only),

1264777862
1264777862
1264777862
1264777862

But I want to get,

1264777862
1264777000

I have followed the question and answer from here and modified from it.

Or maybe there are better methods?

EDIT:

xml,

<?xml version="1.0" encoding="UTF-8" ?>
<records>
    <record timestamp="1264777862">A</record>
    <record>B</record>
    <record timestamp="1264777000">C</record>
    <record>D</record>
</records>

with this,

for ($i=0; $i<$total; $i++)
{
    $value = $x->item($i)->childNodes->item(0)->nodeValue;
    $timestamp = $xpath->evaluate($query,$x->item($i));
    echo $value.': '.$timestamp ."<br/>";
}

I get this result,

A: 1264777862
B: 1264777862
C: 1264777862
D: 1264777862

but this is the result I am after,

A: 1264777862
B: 
C: 1264777862
D: 

EDIT:

a test,

$nodes = $xpath->query('//records/record');

foreach($nodes as $node) {
    $value = $node->nodeValue;
    $timestamp = $node->getAttribute('timestamp');
    echo $value .': '."<br/>";
}

result,

A: 
B: 
C: 
D: 
  • 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-05-26T21:15:06+00:00Added an answer on May 26, 2026 at 9:15 pm

    One method:

    $nodes = $xpath->query('//records[@timestamp]');
    foreach($nodes as $node) {
        $timestamp = $node->getAttribute('timestamp');
    }
    

    Though, you’ve mixed record and records in your example, so I’m not sure which you’re using in reality.


    update: This code works for me:

    <?php
    
    $xml = <<<EOL
    <?xml version="1.0" encoding="UTF-8" ?>
    <records>
        <record timestamp="1264777862">A</record>
        <record>B</record>
        <record timestamp="1264777000">C</record>
        <record>D</record>
    </records>
    EOL;
    
    $x = new DOMDocument();
    $x->loadXML($xml);
    
    $xp = new DOMXpath($x);
    
    $nodes = $xp->query('//records/record');
    foreach($nodes as $node) {
       echo $node->nodeValue, ': ', $node->getAttribute('timestamp'), "\n";
    }
    

    and outputs

    A: 1264777862
    B:
    C: 1264777000
    D:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully, I can get answers for each database server. For an outline of how
How can I iterate over a string in Python (get each character from the
In php how can I read a text file and get each line into
Can I get a list of the playlists and songs in each playlist in
How do you get a list of files within a directory so each can
I can't seem to get this right. class Tree { Node* root; vector& dict;
We have a source XML file that has an address node, and each node
How can I get only the text of the node <p> which has other
Can get all triples with value null in specific field? All people with date_of_birth
I can get easily see what projects and dlls a single project references from

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.