Java XSD installer cannot find element declaration

I have a simple circuit checker method:

// Throws runtime exception if anything goes wrong. public void validate(String schemaURL, String xml) throws Throwable { SAXParserFactory oSAXParserFactory = SAXParserFactory.newInstance(); SAXParser oSAXParser = null; oSAXParserFactory.setNamespaceAware(true); SchemaFactory oSchemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); oSAXParserFactory.setSchema(oSchemaFactory.newSchema(new URL(schemaURL))); oSAXParser = oSAXParserFactory.newSAXParser(); SaxErrorHandler handler = new SaxErrorHandler(); oSAXParser.parse(new InputSource(new StringReader(xml)),handler); } 

I have a circuit hosted at http://myserver.com/schemas/app-config/1.0/app-config-1.0.xsd :

 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="application"> <xs:complexType> <xs:choice> <xs:element ref="info"/> </xs:choice> </xs:complexType> </xs:element> <xs:element name="info"> <xs:complexType> <xs:attribute name="name" type="xs:string" use="optional"/> </xs:complexType> </xs:element> </xs:schema> 

Note the following instance of this circuit:

 <?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://myserver.com/schemas/app-config/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://myserver.com/schemas/app-config/1.0 http://myserver.com/schemas/app-config/1.0/app-config-1.0.xsd"> <info name="Dummy Application" /> </application> 

When I pass the validate method as follows:

 String xmlInstance = readXMLIntoString(); String schemaURL = "http://myserver.com/schemas/app-config/1.0/app-config-1.0.xsd"; validate(schemaURL, xmlInstance); 

I get the following error:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find declaration of element 'application'.

  • Is there something wrong with my circuit?
  • Is something invalid with my instance?
  • Is there a problem with the fact that I really host the schema at the URL (although the one used in this example is a layout, I assure you that the XSD file is indeed hosted at the URL that I find in the code)?

Why can't the validator find application declaration?

+4
source share
2 answers

My initial answer stated that the circuitry was wrong, which actually takes place, but for a different reason than I originally proposed. As Robert targetNamespace said, your scheme targetNamespace not have targetNamespace . Either follow his approach and change your schema, or replace schemaLocation with noNamespaceSchemaLocation .

+2
source

The XML schema is missing a target namespace.

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://myserver.com/schemas/app-config/1.0" elementFormDefault="qualified"> ... 

Instead of declaring targetNamespace in your XML schema, you can remove all of these mumbo jumbo from your XML.

 <application> ... 

Try to stick to the first decision as before.

+3
source

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


All Articles