Using dom4j DOMDocument to feed validator.validate (DOMSource) fails with error in java 1.6 (xsi: noNamespaceSchemaLocation is not allowed), works in 1.5

Using dom4j DOMDocument to feed validator.validate (DOMSource) fails with error in java 1.6 (with xsi: noNamespaceSchemaLocation cannot appear in the root element), works in 1.5

I find the following problem quite unsolvable (well, this is an understatement) - any ideas will be appreciated. Currently, it seems like the best idea is to abandon dom4j in favor of, for example, XOM (http://stackoverflow.com/questions/831865/what-java-xml-library-do-you-recommend-to-replace-dom4j )

I checked in memory the XML created from dom4j 'new DOMDocument ()', but this will not work with Java 6.

The following call to verify (source) DOMSource based on dom4j (1.6.1) DOMDocument works with Java 1.5.x, but does not work with Java 1.6.x:

public void validate() throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setErrorHandler(null); Schema schemaXSD = schemaFactory.newSchema(new URL(getSchemaURLString())); Validator validator = schemaXSD.newValidator(); DOMSource source = new DOMSource(getDocument()); validator.validate(source); } 

getSchemaURLString () is also used to add the xsi: noNamespaceSchemaLocation attribute to the root of the node, i.e.: xsi: noNamespaceSchemaLocation = "http: // localhost: 8080 / integration / xsd / fqlResponseSchema-2.0.xsd"

An exception:

 Exception: org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'specialfields'.; complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'specialfields'. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:417) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3182) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processAttributes(XMLSchemaValidator.java:2659) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:2066) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705) at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:273) at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:240) at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:186) at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:104) at javax.xml.validation.Validator.validate(Validator.java:127) 

Here is the beginning of the XML - generated after disconnecting the validator.validate (source) call:

 <?xml version="1.0" encoding="utf-8"?> <meetings xsi:noNamespaceSchemaLocation="http://localhost:8080/integration/xsd/fqlResponseSchema-2.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> ............. </meetings> 

And XSD:

 <?xml version="1.0" encoding="utf-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="meetings"> <xs:complexType> <xs:choice> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" ref="summary" /> <xs:element minOccurs="0" maxOccurs="unbounded" ref="meeting" /> </xs:sequence> <xs:element ref="error" /> </xs:choice> </xs:complexType> </xs:element> <xs:element name="summary"> ................ 

So my root element is rejected as it contains the xsi: noNamespaceSchemaLocation attribute. And the circuit itself does not indicate that as a valid attribute of my root element?

At the moment, it seems to me that I need to abandon dom4j for this task and switch to one of the other solutions, for example, as described here:

But I would like to know what I did wrong!

Thanks in advance.

+4
source share
2 answers

I had the same issue and found the following documentation in

http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi/index.html

Confirm documented schema mapping

Some documents indicate the scheme to which they are awaiting verification, usually using xsi: noNamespaceSchemaLocation and / or xsi: schemaLocation, such as:

 <document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.example.com/document.xsd"> ... 

If you create a schema without specifying a URL, file, or source, then the Java language creates one that looks in the document being to find the schema that it should use. For instance:

 SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = factory.newSchema(); 

However, as a rule, this is not what you want. Typically, a consumer should choose a document rather than a document producer. In addition, this approach only works for XSD. All other languages ​​schemas require an explicitly specified schema location.

+2
source

The reason is that a non-supportive JAXP SAXParser namespace is created and used (see link ).

And the solution for different libs that I found at www.edankert.com .

0
source

Source: https://habr.com/ru/post/1340814/


All Articles