javax.xml.bind.UnmarshalException: unexpected element. Expected elements are (none) - java

I am getting this error, while unmarshalling a string. I have created Java files using JAXB.
JAXBContext jaxbContext = JAXBContext.newInstance(DocumentType.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
// Input string posted below
DocumentType dType = (DocumentType) unmarshaller.unmarshal(new StringReader("input string"));
Stack trace:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd", local:"document"). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1048)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3065)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:881)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
at JaxbTest.main(JaxbTest.java:19)
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd"
elementFormDefault="qualified">
<xs:element name="document" type="tns:documentType"></xs:element>
<xs:complexType name="documentType">
<xs:sequence>
<xs:element name="businessCard" type="tns:businessCardType" minOccurs="0" maxOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="businessCardType">
<xs:sequence>
<xs:element name="field" type="tns:fieldType" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fieldType">
<xs:sequence>
<xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1">
</xs:element>
<xs:element name="characters" type="tns:charactersType" minOccurs="0" maxOccurs="1"></xs:element>
<xs:element name="fieldComponents" type="tns:fieldComponentsType" minOccurs="0" maxOccurs="1"></xs:element>
</xs:sequence>
<xs:attribute name="type" type="tns:typeType"></xs:attribute>
</xs:complexType>
<xs:simpleType name="typeType">
<xs:restriction base="xs:string">
<xs:enumeration value="Name"></xs:enumeration>
<xs:enumeration value="Phone"></xs:enumeration>
<xs:enumeration value="Mobile"></xs:enumeration>
<xs:enumeration value="Fax"></xs:enumeration>
<xs:enumeration value="Company"></xs:enumeration>
<xs:enumeration value="Job"></xs:enumeration>
<xs:enumeration value="Address"></xs:enumeration>
<xs:enumeration value="Email"></xs:enumeration>
<xs:enumeration value="Web"></xs:enumeration>
<xs:enumeration value="Text"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="charactersType">
<xs:sequence>
<xs:element name="char" type="tns:charType" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="charType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="suspicious" type="xs:boolean" use="optional"></xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="fieldComponentsType">
<xs:sequence>
<xs:element name="fieldComponent" type="tns:fieldComponentType" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fieldComponentType">
<xs:sequence>
<xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1">
</xs:element>
</xs:sequence>
<xs:attribute name="type" type="tns:fieldComponentTypeType"></xs:attribute>
</xs:complexType>
<xs:simpleType name="fieldComponentTypeType">
<xs:restriction base="xs:string">
<xs:enumeration value="FirstName"></xs:enumeration>
<xs:enumeration value="MiddleName"></xs:enumeration>
<xs:enumeration value="LastName"></xs:enumeration>
<xs:enumeration value="ExtraName"></xs:enumeration>
<xs:enumeration value="PhonePrefix"></xs:enumeration>
<xs:enumeration value="PhoneCountryCode"></xs:enumeration>
<xs:enumeration value="PhoneCode"></xs:enumeration>
<xs:enumeration value="PhoneBody"></xs:enumeration>
<xs:enumeration value="PhoneExtension"></xs:enumeration>
<xs:enumeration value="ZipCode"></xs:enumeration>
<xs:enumeration value="Country"></xs:enumeration>
<xs:enumeration value="City"></xs:enumeration>
<xs:enumeration value="StreetAddress"></xs:enumeration>
<xs:enumeration value="JobPosition"></xs:enumeration>
<xs:enumeration value="JobDepartment"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Input string:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd">
<businessCard>
<field type="Phone">
<value>783-37-00</value>
</field>
<field type="Phone">
<value>783-37-01</value>
</field>
<field type="Fax">
<value>783-26-63</value>
</field>
<field type="Email">
<value>john_sm#abbyy.com</value>
</field>
<field type="Web">
<value>www.abbyy.com</value>
</field>
<field type="Address">
<value>Otradnaya str., 2b, bld.6, 127273, Moscow, Russia</value>
</field>
<field type="Name">
<value>John Smith</value>
</field>
<field type="Company">
<value>ABBYY Headquarters</value>
</field>
<field type="Job">
<value>Product Analyst</value>
</field>
<field type="Text">
<value>ABBYY ABBYY Headquarters John Smith Product Analyst ABBYY Headquarters Otradnaya str., 2b, bld.6, 127273, Moscow, Russia Tel: 783-37-00 Fax: 783-26-63 john_sm#abbyy.com www.abbyy.com </value>
</field>
</businessCard>
</document>

When you generate a JAXB model from an XML Schema, global elements that correspond to named complex types will have that metadata captured as an #XmlElementDecl annotation on a create method in the ObjectFactory class. Since you are creating the JAXBContext on just the DocumentType class this metadata isn't being processed. If you generated your JAXB model from an XML Schema then you should create the JAXBContext on the generated package name or ObjectFactory class to ensure all the necessary metadata is processed.
Example solution:
JAXBContext jaxbContext = JAXBContext.newInstance(my.generatedschema.dir.ObjectFactory.class);
DocumentType documentType = ((JAXBElement<DocumentType>) jaxbContext.createUnmarshaller().unmarshal(inputStream)).getValue();

Alternatively if you want to persist in using the DocumentType class.
Then you could just add the following annotation on top of your DocumentType class.
#XmlRootElement(name="document")
Note: the String value "document" refers to the name of the root tag of the xml message.

One of the reasons for this error is the use of the jaxb implementation from the jdk. I am not sure why such a problem can appear in pretty simple xml parsing situations. You may use the latest version of the jaxb library from a public maven repository:
http://mvnrepository.com
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.12</version>
</dependency>

In my case, I got this error when I imported incorrectly jakarta.xml.bind.annotation.XmlRootElement. The correct one should be javax.xml.bind.annotation.XmlRootElement

In our case we were getting UnmarshalException because a wrong Java package was specified in the following. The issue was resolved once the right package was in place:
#Bean
public Unmarshaller tmsUnmarshaller() {
final Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller
.setPackagesToScan("java.package.to.generated.java.classes.for.xsd");
return jaxb2Marshaller;
}

Related

XML namespace inheritance

Let's say I have following xsd schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="kom">
<xs:complexType>
<xs:sequence>
<xs:element name="um">
<xs:complexType>
<xs:attribute name="odd"
type="TypKod"
use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="version" use="required">
<xs:simpleType>
<xs:restriction base="xs:normalizedString">
<xs:length value="3"/>
<xs:enumeration value="1.0"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="od" type="TypKod" use="required"/>
</xs:complexType>
</xs:element>
<xs:simpleType name="TypKod">
<xs:restriction base="xs:normalizedString">
<xs:length value="2" fixed="true"/>
<xs:enumeration value="01"/>
</xs:restriction>
</xs:simpleType>
And I have following soap UI envelope:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:b="http://xml.daniel.pl/b"
xmlns:c="http://xml.daniel.pl/c"
xmlns:xm="http://www.w3.org/2005/05/xmlmime">
<soapenv:Header/>
<soapenv:Body>
<b:execute>
<b:date>2013-12-06T12:32:41.106+01:00</:date>
<b:pay>
<b:text>
<p:kom xmlns:p="http://daniel/p" version="1.0" od="01">
<um odd="01"/>
</p:kom>
</b:text>
</b:payload>
</b:execute>
</soapenv:Body>
</soapenv:Envelope>
The question is, shouldn't it be
<p:um odd="01"/>
instead of
<um odd="01"/>
?
Element um belongs to namespace 'p' but i don't know how to prepare schema.
In code generated by axis I have:
if (reader.isStartElement() && new javax.xml.namespace.QName("","um").equals(reader.getName()))
so there is empty string before 'um'. How can I make it to put there my namespace 'p' without manual modification of classes generated by axis?
When I put <p:um odd="01"/> in the envelope I get:
Caused by: java.lang.Exception: org.apache.axis2.databinding.ADBException: Unexpected subelement {http://daniel/p}um

xml schema and java objects

I have received xml schema from a third party vendor. However the xml schema and the xml don't seem to match atleast that is my guess. I am using Intellij and use the JAXB plugin to get JAVA objects.
My code never returns the field values i.e. it never returns "my string value" back from xml
<field type="Address">
<value type="Value">my string value</value>
</field>
I get data back as
0 = {FieldType#1372}
characters = null
value = {FieldTypeType#1381} "ADDRESS"
value = {String#1383} "Address"
data = null
name = {String#1382} "ADDRESS"
ordinal = 8
1 = {FieldType#1373}
characters = null
value = {FieldTypeType#1387} "CARD_NUMBER"
2 = {FieldType#1374}
characters = null
value = {FieldTypeType#1389} "DATE"
3 = {FieldType#1375}
I have the xml as this (part of it)
<field type="Address">
<value type="Value">my string value</value>
</field>
<field type="CardNumber">
<value type="Value">xxxxx</value>
</field>
<field type="Date">
<value type="Day">21</value>
<value type="Month">4</value>
<value type="Year">2015</value>
</field>
<field type="Phone">
<value type="Value">212XXX</value>
</field>
<field type="SubTotal">
<value type="value">XXX</value>
</field>
and schema is
<xs:complexType name="receiptType">
<xs:sequence>
<xs:element name="field" type="tns:fieldType" minOccurs="0" maxOccurs="unbounded"></xs:element>
<xs:element name="lineItem" type="tns:lineItemType" minOccurs="0" maxOccurs="unbounded"></xs:element>
<xs:element name="recognizedText" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
</xs:sequence>
<xs:attribute name="paymentType" type="tns:paymentTypeType"></xs:attribute>
<xs:attribute name="cardType" type="tns:cardTypeType"></xs:attribute>
<xs:attribute name="currency" type="xs:string"></xs:attribute>
<xs:attribute name="purchaseType" type="xs:string"></xs:attribute>
</xs:complexType>
<xs:simpleType name="paymentTypeType">
<xs:restriction base="xs:string">
<xs:enumeration value="Card"></xs:enumeration>
<xs:enumeration value="Cash"></xs:enumeration>
<xs:enumeration value="Undefined"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="cardTypeType">
<xs:restriction base="xs:string">
<xs:enumeration value="AmericanExpress"></xs:enumeration>
<xs:enumeration value="EuroCard"></xs:enumeration>
<xs:enumeration value="MasterCard"></xs:enumeration>
<xs:enumeration value="Visa"></xs:enumeration>
<xs:enumeration value="Undefined"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="fieldType">
<xs:sequence>
<xs:element name="value" type="tns:valueType" minOccurs="1" maxOccurs="unbounded">
</xs:element>
<xs:element name="characters" type="tns:charactersType" minOccurs="0" maxOccurs="1"></xs:element>
</xs:sequence>
<xs:attribute name="value" type="tns:fieldTypeType"></xs:attribute>
</xs:complexType>
<xs:complexType name="valueType">
<xs:attribute name="type" type="tns:valueTypeType"></xs:attribute>
</xs:complexType>
<xs:simpleType name="valueTypeType">
<xs:restriction base="xs:string">
<xs:enumeration value="Value"></xs:enumeration>
<xs:enumeration value="Day"></xs:enumeration>
<xs:enumeration value="Month"></xs:enumeration>
<xs:enumeration value="Year"></xs:enumeration>
<xs:enumeration value="Hour"></xs:enumeration>
<xs:enumeration value="Minute"></xs:enumeration>
<xs:enumeration value="Second"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="fieldTypeType">
<xs:restriction base="xs:string">
<xs:enumeration value="Total"></xs:enumeration>
<xs:enumeration value="SubTotal"></xs:enumeration>
<xs:enumeration value="CardNumber"></xs:enumeration>
<xs:enumeration value="TotalTax"></xs:enumeration>
<xs:enumeration value="Tax"></xs:enumeration>
<xs:enumeration value="Date"></xs:enumeration>
<xs:enumeration value="Time"></xs:enumeration>
<xs:enumeration value="Vendor"></xs:enumeration>
<xs:enumeration value="Address"></xs:enumeration>
<xs:enumeration value="Phone"></xs:enumeration>
<xs:enumeration value="Fax"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
Appreciate all the help!

My XSD does not validate the XML despite proper namespace declarations

The XSD <schema> tag:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.cmu.edu/ns/blank"
targetNamespace="http://www.cmu.edu/ns/blank"
elementFormDefault="qualified">
The root of the XML, <people> tag.
<people
xmlns="http://www.cmu.edu/ns/blank"
xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.cmu.edu/ns/blank student.xsd">
How ever I get en error that:
cvc-elt.1.a: Cannot find the declaration of element 'people'
I know it has something to do with namespaces but I can not figure out what.
Please help
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!-- <!DOCTYPE people SYSTEM "validator.dtd"> -->
<people
xmlns="http://www.cmu.edu/ns/blank"
xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.cmu.edu/ns/blank student.xsd">
<student>
<name>John</name>
<course>Computer Technology</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
<student>
<name>Foo</name>
<course>Industrial Electronics</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
</people>
XSD
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.cmu.edu/ns/blank"
targetNamespace="http://www.cmu.edu/ns/blank"
elementFormDefault="qualified">
<xs:element name="people">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="course" type="xs:string" />
<xs:element name="semester">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1" />
<xs:enumeration value="2" />
<xs:enumeration value="3" />
<xs:enumeration value="4" />
<xs:enumeration value="5" />
<xs:enumeration value="6" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="scheme">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value = "E|C" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The only thing that's not working is the schema location attribute's namespace. The XML Schema Instance namespace is:
http://www.w3.org/2001/XMLSchema-instance
instead of:
http://www.w3c.org/2001/XMLSchema-instance (you put a c in w3c)
For the given XML, the error is
The 'http://www.w3c.org/2001/XMLSchema-instance:schemaLocation' attribute is not declared.

Error while generating JAXB model from xsd in eclipse

I am trying to generate JAXB model from the following xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://abc.com/mmm/trt" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:annotation>
<xs:documentation>Schema for Cache Refresh Event.
</xs:documentation>
</xs:annotation>
<xs:simpleType name="cacheCode">
<xs:restriction base="xs:string">
<xs:enumeration value="BUSINESS_RULE" />
<xs:enumeration value="USER" />
</xs:restriction>
</xs:simpleType>
<xs:element name="cacheRefreshEvent">
<xs:complexType>
<xs:sequence>
<xs:element name="cacheCode" type="cacheCode" minOccurs="1"
maxOccurs="1" />
<xs:element name="entityRefId" type="xs:string" minOccurs="0"
maxOccurs="20" />
<xs:element name="cacheRefreshDate" type="xs:dateTime"
minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But, it is giving me the following error, which i got in Eclipse console
parsing a schema...
[ERROR] src-resolve.4.1: Error resolving component 'cacheCode'. It was detected that 'cacheCode' has no namespace, but components with no target namespace are not referenceable from schema document 'file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd'. If 'cacheCode' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'cacheCode' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd'.
line 18 of file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd
Failed to parse a schema.
Please help.
Remove this extra namespace attribute targetNamespace="http://www.hcentive.com/mea/trr"
OR add this xmlns="http://www.hcentive.com/mea/trr"
Both solutions will work :)
For you reference targetnamespace-and-xmlns

XML validation against XSD: cvc-elt.1: Cannot find the declaration of element 'xxx'

I have the following XML file:
<?xml version="1.0"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library
xmlns="http://example.com/a"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com library.xsd"
name=".NET Developer's Library">
<book>
<category>computerss</category>
<title>Programming Microsoft .NET</title>
<author>Jeff Prosise</author>
<isbn>0-7356-1376-1</isbn>
</book>
<book>
<category>computer</category>
<title>Microsoft .NET for Programmers</title>
<author>Fergal Grimes</author>
<isbn>1-930110-19-7</isbn>
</book>
</library>
And the following Java code:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
docBuilderFactory.setSchema(sf.newSchema(new File("library.xsd")));
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.parse(new FileInputStream("data.xml"));
It produces the following error:
[Error] :7:33: cvc-elt.1: Cannot find the declaration of element 'library'.
If I remove the XSD declaration in the XML file everything works fine...
Any inside highly appreciated. Thanks.
And here is the schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="category"/>
<xs:element ref="title"/>
<xs:element ref="author"/>
<xs:element ref="isbn"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="category">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="computer" />
<xs:enumeration value="poetry" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
</xs:schema>
Your XML has a reference to namespace (xmlns="http://example.com/a") which is not the same as in your schema. Have you tried to validate your XML against schema in any XML editor (e.g. Altova or Eclipse etc).
So far it looks like your parsing error is legit, XML is not valid according to the schema.
Your schema definition is incorrect. For a start, as maximdim says, your schema has no targetNamespace="http://mysite.com/a" attribute in the schema tag.
Secondly your schema looks as if it should only have a single root element, yours has 6.
A correct schema for your XML instance would be:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://mysite.com/a" targetNamespace="http://mysite.com/a">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="book" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="book">
<xs:sequence>
<xs:element name="category">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="computer" />
<xs:enumeration value="poetry" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

Categories