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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:16:54+00:00 2026-05-13T10:16:54+00:00

About a year ago I wrote a jQuery-inspired library which allowed you to manipulate

  • 0

About a year ago I wrote a jQuery-inspired library which allowed you to manipulate the DOM using PHP’s XPath and DOMDocument. I recently wanted to clean it up and post it as an open source project. I’ve been spending the past few days making improvements and implementing some more of PHP’s native OO features.

Anyhow, I thought I’d add a new method which allows you to merge a separate XML document with the current one. The catch here is that this method asks for 2 XPath expressions. The first one fetches the elements you want to merge into the existing document. The second specifies the destination path of these merged elements.

The method works well in fetching matching elements from both paths, but I’m having issues with importing the foreign elements into the current DOM. I keep getting the dreaded ‘Wrong Document Error’ message.

I thought I knew what I was doing, but I suppose I was wrong. If you look at the following code, you can see that I’m first iteration through the current documents matching elements, then through the foreign document’s matching elements.

Within the second nested loop is where I am attempting to merge each foreign element into the destination path in the current document.

Not sure what I’m doing wrong here as I’m clearly importing the foreign node into the current document before appending it.

public function merge($source, $path_origin, $path_destination)
{
    $Dom = new self;

    if(false == $Dom->loadXml($source))
    {
        throw new DOMException('XML source could not be loaded into the DOM.');
    }

    $XPath = new DOMXPath($Dom);

    foreach($this->path($path_destination, true) as $Destination)
    {
        if(false == in_array($Destination->nodeName, array('#text', '#document')))
        {
            foreach($XPath->query($path_origin) as $Origin)
            {
                if(false == in_array($Destination->nodeName, array('#text', '#document')))
                {
                    $this->importNode($Origin, true);

                    $Destination->appendChild($Origin->cloneNode(true));
                }
            }
        }
    }

    return $this;
}

You can find the library in its entirety in the following Github repo:

http://github.com/wilhelm-murdoch/DomQuery

Halps!!!

  • 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-13T10:16:55+00:00Added an answer on May 13, 2026 at 10:16 am

    importNode doesn’t “change” the node so it belongs to another document. It creates a new node belonging to the new document and returns it. So you should be getting its return value and using that in appendChild.

    $Destination->appendChild($this->importNode($Origin, true));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

About a year ago, I picked up Scott Ambler's Refactoring Databases: Evolutionary Database Design
I switched to the dvorak keyboard layout about a year ago. I now use
I remember reading a post about a year or so ago by Scott Hanselman
I've been using Git on Linux for about a year, and everything works fine.
I used SPRY about a year ago to experiment with the fact that you
This was asked about a year ago , but never really answered. It would
About a year ago, a question was asked on stackoverflow about how to horizontally
About a year ago I placed some media and some web pages under my
I remember that for about a year ago I did some merges that resulted
A similar question was asked and answered about a year ago, but was either

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.