What is wrong with this XML / XSD?

I have simple XSD and even simpler XML. But Java 2 XML validation fails. (using javax.xml.validation)

Here is my XSD:

<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://foo.com/darnit" targetNamespace="http://foo.com/darnit"> <xsd:element name="Person" type="tns:PersonType"/> <xsd:simpleType name="nameType"> <xsd:restriction base="xsd:string"/> </xsd:simpleType> <xsd:complexType name="PersonType"> <xsd:sequence> <xsd:element minOccurs="1" maxOccurs="2" name="FirstName" type="tns:nameType"/> <xsd:element minOccurs="1" maxOccurs="1" name="LastName" type="tns:nameType"/> </xsd:sequence> </xsd:complexType> </xsd:schema> 

And here is an example XML:

 <?xml version="1.0" encoding="UTF-8"?> <Person xmlns="http://foo.com/darnit"> <FirstName>John</FirstName> <FirstName>Michael</FirstName> <LastName>Smith</LastName> </Person> 

Here is the error message I get:

 org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'FirstName'. One of '{FirstName}' is expected. at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.jaxp.validation.StreamValidatorHelper.validate(Unknown Source) at org.apache.xerces.jaxp.validation.XMLSchemaValidator.validate(Unknown Source) at javax.xml.validation.Validator.validate(Unknown Source) 

If I qualify XML with a namespace prefix, it works!

 <?xml version="1.0" encoding="UTF-8"?> <foo:Person xmlns:foo="http://foo.com/darnit"> <FirstName>John</FirstName> <FirstName>Michael</FirstName> <LastName>Smith</LastName> </foo:Person> 

But my XSD allows you to use unskilled items!

Do I need to set a property in SchemaFactory, Schema or Validator?

Thanks.

+4
source share
1 answer

Add elementFormDefault = qualified for you, such as:

 <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://foo.com/darnit" targetNamespace="http://foo.com/darnit" elementFormDefault="qualified"> 

Then all items will be in the default target namespace.

All globally declared elements will belong to the target namespace. However, the attribute "elementFormDefault" controls the ownership or non-local elements also belong to the target namespace, that is, "are qualified". Some people obviously prefer the β€œunconditional” style that you inadvertently created. However, I have never seen good arguments in favor of this.

+2
source

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


All Articles