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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:53:36+00:00 2026-05-23T02:53:36+00:00

I have 2 Document objects with documents that contain similiar XML’s. For example: <tt:root

  • 0

I have 2 Document objects with documents that contain similiar XML’s. For example:

<tt:root xmlns:tt="http://myurl.com/">
  <tt:child/>
  <tt:child/>
</tt:root>

And the other one:

<ns1:root xmlns:ns1="http://myurl.com/" xmlns:ns2="http://myotherurl.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ns1:child/>
  <ns1:child xsi:type="ns2:SomeType"/>
</ns1:root>

I need to merge them to 1 document with 1 root element and 4 child elements.
Problem is, if I use document.importNode function to do the merging, it properly handles the namespaces everywhere BUT xsi:type element. So what I’m getting in result is this:

<tt:root xmlns:tt="http://myurl.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <tt:child/>
  <tt:child/>
  <ns1:child xmlns:ns1="http://myurl.com/"/>
  <ns1:child xmlns:ns1="http://myurl.com/" xsi:type="ns2:SomeType"/>
</tt:root>

As you can see, ns2 is used in xsi:type but is not defined anywhere. Is there any automated way to solve this problem?

Thanks.

ADDED:

If this task is impossible to complete using the default java DOM libraries, maybe there is some other library I can use to complete my task?

  • 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-23T02:53:37+00:00Added an answer on May 23, 2026 at 2:53 am

    A single-line of XQuery could do the job: construct a new node named as the context root element, then import its children together with those from the other document:

    declare variable $other external; element {node-name(*)} {*/*, $other/*/*}
    

    Though in XQuery you don’t have full control over namespace nodes (at least in XQuery 1.0), it has a copy-namespaces mode setting that can be used to ask for keeping the namespace context intact, in case the implementation does preserve it by default.

    If XQuery is a viable option, then saxon9he.jar could be the “magic xml library” that you are after.

    Here is sample code exposing some context, using the s9api API:

    import javax.xml.parsers.DocumentBuilderFactory;
    import net.sf.saxon.s9api.*;
    import org.w3c.dom.Document;
    
    ...
    
      Document merge(Document context, Document other) throws Exception
      {
        Processor processor = new Processor(false);
        XQueryExecutable executable = processor.newXQueryCompiler().compile(
          "declare variable $other external; element {node-name(*)} {*/*, $other/*/*}");
        XQueryEvaluator evaluator = executable.load();    
        DocumentBuilder db = processor.newDocumentBuilder();
        evaluator.setContextItem(db.wrap(context));
        evaluator.setExternalVariable(new QName("other"), db.wrap(other));
        Document doc =
          DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        processor.writeXdmValue(evaluator.evaluate(), new DOMDestination(doc));
        return doc;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml document object that I need to convert into a string.
I have a document and want to create it as a twocolumn-document. That works
I have an XML document with un-namespaced elements, and I want to use XSLT
I have an issue while trying to parse an Xml to Objects using Linq
I have an object I am using to store document meta data into a
A have the document with 23 figures and 46 tables (appendix to journal article).
I have a document with headings and unordered lists. How can I use JQuery
I have a document with connection information to a device over a serial port.
Scenario: I have a document I created using LaTeX (my resume in this case),
As everyone knows, sometimes developers have to document stuff. Or capture some stuff for

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.