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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:34:21+00:00 2026-05-11T08:34:21+00:00

Ok, so I had this neat little idea the other night to create a

  • 0

Ok, so I had this neat little idea the other night to create a helper class for DOMDOCUMENT that mimics, to some extent, jQuery’s ability to manipulate the DOM of an HTML or XML-based string. Instead of css selectors, XPath is used. For example:

$Xml->load($source)     ->path('//root/items')     ->each(function($Context)     {         echo $Context->nodeValue;     }); 

This would invoke a callback function on every resulting node. Unfortunately, PHP version < 5.3.x doesn’t support lambda functions or closures, so I’m forced to do something a bit more like this for the time being:

$Xml->load($source)     ->path('//root/items')     ->walk('printValue', 'param1', 'param2'); 

Everything is working great at the moment and I think this project would be useful to a lot of people, but I’m stuck with one of the functions. I am attempting to mimic jQuery’s ‘replace’ method. Using the following code, I can accomplish this quite easily by applying the following method:

$Xml->load($source)     ->path('//root/items')     ->replace($Xml->createElement('foo', 'bar')); // can be an object, string or XPath pattern 

The code behind this method is:

public function replace($Content) {     foreach($this->results as $Element)     {         $Element->parentNode->appendChild($Content->cloneNode(true));         $Element->parentNode->removeChild($Element);     }      return $this; } 

Now, this works. It replaces every resulting element with a cloned version of $Content. The problem is that it adds them to the bottom of the parent node’s list of children. The question is, how do I clone this element to replace other elements, while still retaining the original position in the DOM?

I was thinking about reverse-engineering the node I was to replace. Basically, copying over values, attributes and element name from $Content, but I am unable to change the actual element name of the target element.

Reflection could be a possibility, but there’s gotta be an easier way to do this.

Anybody?

  • 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. 2026-05-11T08:34:22+00:00Added an answer on May 11, 2026 at 8:34 am

    Use replaceChild instead of appendChild/removeChild.

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

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer AppDomain is pretty much like a process, it's an infrastructure… May 11, 2026 at 2:18 pm
  • added an answer Replace string strTemp = ' KEY Text, VALUE Text ';… May 11, 2026 at 2:18 pm
  • added an answer The important point is that Golomb codes are not meant… May 11, 2026 at 2:18 pm

Related Questions

I had no luck with this question so I've produced this simple-as-possible-test-case to demonstrate
Ok, I had a simple layout problem a week or two ago. Namely sections
Ok this is a general question about how to solve this issue, not to
Imagine in the Global.asax.cs file I had an instance class as a private field.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.