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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:05:13+00:00 2026-05-27T01:05:13+00:00

I am trying to iterate over set of nodes given by xpath and set

  • 0

I am trying to iterate over set of nodes given by xpath and set certain attribute for each node. However it works only for nodes withou content or with empty (whitespace) content. I have tried 2 approaches but with the same result (maybe they are both the same on some deeper level, dunno). The commented line is the second approach.

$temp = simplexml_load_string (
'<toolbox>
    <hammer/>
    <screwdriver>   </screwdriver>
    <knife>
        sharp
    </knife>
</toolbox>' );

echo "vanilla toolbox: ";
print_r($temp);

$nodes = $temp->xpath('//*[not(@id)]');

foreach($nodes as $obj) {
    $tempdom = dom_import_simplexml($obj);
    $tempdom->setAttributeNode(new DOMAttr('id', 5));
    //$obj->addAttribute('bagr', 5);
}

echo "processed toolbox: ";
print_r($temp);

This is output. Attribute id is missing in node knife.:

vanilla toolbox: SimpleXMLElement Object
(
[hammer] => SimpleXMLElement Object
    (
    )

[screwdriver] => SimpleXMLElement Object
    (
        [0] =>    
    )

[knife] => 
        sharp

)
processed toolbox: SimpleXMLElement Object
(
[@attributes] => Array
    (
        [id] => 5
    )

[hammer] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [id] => 5
            )

    )

[screwdriver] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [id] => 5
            )

        [0] =>    
    )

[knife] => 
        sharp
  • 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-27T01:05:14+00:00Added an answer on May 27, 2026 at 1:05 am

    I’m unable to reproduce what you describe, the changed XML is:

    <?xml version="1.0"?>
    <toolbox id="5">
        <hammer id="5"/>
        <screwdriver id="5">   </screwdriver>
        <knife id="5">
            sharp
        </knife>
    </toolbox>
    

    Demo

    It’s exactly your code, maybe you’re using a different LIBXML version? See the LIBXML_VERSION constant (codepad viper has 20626 (2.6.26)).

    But probably it’s just only the print_r output for a SimpleXMLElement object.

    It does not output the attributes for the last element, even on a brand new object, but it’s still possible to access the attribute. Demo.

    You will see when you print_r($temp->knife['id']); that the attribute is set (as you can see in the earlier XML output).

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

Sidebar

Related Questions

I'm trying to iterate over an enum, and call a method using each of
I'm trying to iterate over checkboxes in an ASP.Net web page, and if any
I'm trying to iterate over all main menu's items. Problem starts at sub menus
I'm trying to find out how I can iterate over the final results of
I've been trying to figure out how to iterate over the list of columns
I'm trying to write an algorithm that will let me iterate over all desired
I'm trying to iterate through all nodes, so I can print them out for
I'm trying to iterate through a set of keys in a properties file, so
I am looking for an automated way to iterate over all ObjectQueries and set
I'm trying to using numpy.lib.stride_tricks.as_strided to iterate over non-overlapping blocks of an array, but

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.