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));
}
}