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

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer So according to http://idea.uab.es/mcreel/ParallelKnoppix/, on PelicanHPC "The LAM-MPI and OpenMPI… May 16, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer Absurdly, changing /git/Lunch.git/lunch-receive > lunch.txt to: /git/Lunch.git/lunch-receive > lunch.txt 2>&1… May 16, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer select lt.user, lt.team, lt.league, l.game, l.type, l.name from tbl_leaguetables lt… May 16, 2026 at 4:53 pm

Trending Tags

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

Top Members

Related Questions

Maybe it's me, but it appears that if you have an XSD <?xml version=1.0
I have found several sources regarding how to validate an xml document against a
I have some XML and an XML Schema in a local xsd file. The
I have an xml document with three child elements repeating in any order. I
I have a simple doc.xml file which contains a single root element with a
I have created an XML schema (foo.xsd) and used xjc to create my binding
I have generated an XSD file for an XML file and it's working when
I have a hibernate project, which uses JPA. my persistence.xml contents is as follows:
I have the following in my XSL which adds a xmlns to my XML.
I have written a small XML validator, that takes in an XML file and

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.