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 66k
  • Answers 66k
  • 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 Do you have access to the Exchange server via IMAP… May 11, 2026 at 11:23 am
  • added an answer It's a little bit hard to tell what's going wrong… May 11, 2026 at 11:23 am
  • added an answer See OPENXML. May 11, 2026 at 11:23 am

Related Questions

Ok, so I demo'd Refactor Pro and Resharper, I'm more comfortable with Resharper so
Ok, so I just ran into the following problem that raised an eyebrow. For
Ok, so I have an nHibernate ICriteria that returns an object. I want to
Ok, so I'm building bread crumbs and depending on the value of the breadcrumb
OK, so I couldn't really think of an apropos title that summarizes this. The
Ok, so I have this regex: ( |^|>)(((((((\+|00)(31|32)( )?(\(0\))?)|0)([0-9]{2})(-)?( )?)?)([0-9]{7}))|((((((\+|00)(31|32)( )?(\(0\))?)|0)([0-9]{3})(-)?( )?)?)([0-9]{6}))|((((((\+|00)(31|32)( )?(\(0\))?)|0)([0-9]{1})(-)?( )?)?)([0-9]{8})))(
Ok, so I'm looking for a bit of architecture guidance, my team is getting
OK, so I'm trying to teach myself the CakePHP framework, and I'm trying to
Ok, so I want to get a webapp running in tomcat (5.5) to run
Ok, so i have been trying to put everyone one of my classes in

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.