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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:23:48+00:00 2026-05-13T17:23:48+00:00

We have an XSD which validates an XML document with a namespace declared, but

  • 0

We have an XSD which validates an XML document with a namespace declared, but we would like to use it on a document without one.

In Java 5, it looks like it’s possible to call setAttribute() on the xmlns attribute of the root element, but this fails in Java 6 with an exception:

Exception in thread "main" org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Test'.

Is this a bug in Java 5 or 6 or neither?

Code to reproduce:

import java.io.*;
import javax.xml.XMLConstants;
import javax.xml.parsers.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.validation.*;
import org.w3c.dom.Document;

public class NamespaceTest
{
    public static void main(String[] args) throws Exception
    {
        String namespace = "myNamespace";
        String xmlDoc = "<Test/>\n";
        String xsd = String.format(
                "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"%n" +
                "            targetNamespace=\"%1$s\" xmlns=\"%1$s\" elementFormDefault=\"qualified\">%n" +
                "     <xs:element name=\"Test\"/>%n" +
                "</xs:schema>%n", namespace);

        System.out.println("Original doc:\n" + xmlDoc);
        System.out.println("Original schema:\n" + xsd);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document testXml = builder.parse(new ByteArrayInputStream(xmlDoc.getBytes("UTF-8")));
        testXml.getDocumentElement().setAttribute("xmlns", namespace);
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(new StreamSource(new StringReader(xsd)));
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(testXml));

    }   
}
  • 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-13T17:23:49+00:00Added an answer on May 13, 2026 at 5:23 pm

    This does not look like a bug to me, and I am surprised that you could do this earlier. Setting the xmlns attribute on the root element after the DOM tree has already been built is too late.

    At that point, every element in the DOM tree already has had its prefix resolved, and a local name and URI assigned.

    If you really want to do this by manipulating the xmlns attribute (which is not really a proper attribute), you have to manipulate the input stream before calling the DOM parser.

    Alternatively, could try to walk the tree and set the namespace URI of each node in it.

    Edit: to clarify, if there was a bug in there somewhere, it was in Java 5 – if somebody checked for an xmlns attribute. The correct way is to get the URI of the root element.

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

Sidebar

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.