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.
Related
I've made a mistake but I can't see what it is...
I'm trying to validate a piece of XML (a SOAP message) using Java, against an XSD. It's failing on what I think should be a valid piece of XML/message with the Exception:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'tns:Envelope'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203).
Note 1: I've debugged xmlSource and xsdSource and they contain the expected Readers with the expected Strings.
Note 2: The content of the XML cannot be changed. I am a consumer only.
Note 3: As the XML is a SOAP message I grabbed the XSD from the Web. This therefore could be incorrect.
Note 4: I've simplified the incoming message for brevity but get the same error with the "actual" message.
Java
#Override
public boolean isMessageValid(final String xml) {
try {
final Source xmlSource = getStreamSource(xml);
final Source xsdSource = getStreamSource(fileService.getResourceFileAsString("xsd/soap-envelope.xsd"));
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Schema schema = schemaFactory.newSchema(xsdSource);
final Validator validator = schema.newValidator();
validator.validate(xmlSource);
return true;
} catch (final Exception ex) {
LOG.error("Exception whilst validating message", ex);
return false;
}
}
private StreamSource getStreamSource(final String xml) {
return new StreamSource(new StringReader(xml));
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<tns:Envelope xsi:schemaLocation="http://www.w3.org/2001/12/soap-envelope soap-envelope.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://www.w3.org/2001/12/soap-envelope">
<tns:Body>
<!-- snipped -->
<someThing>else</someThing>
</tns:Body>
</tns:Envelope>
XSD
<?xml version='1.0' encoding='UTF-8' ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/"
targetNamespace="http://schemas.xmlsoap.org/soap/envelope/" >
<!-- Envelope, header and body -->
<xs:element name="Envelope" type="tns:Envelope" />
<xs:complexType name="Envelope" >
<xs:sequence>
<xs:element ref="tns:Header" minOccurs="0" />
<xs:element ref="tns:Body" minOccurs="1" />
<xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
</xs:sequence>
<xs:anyAttribute namespace="##other" processContents="lax" />
</xs:complexType>
<xs:element name="Header" type="tns:Header" />
<xs:complexType name="Header" >
<xs:sequence>
<xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
</xs:sequence>
<xs:anyAttribute namespace="##other" processContents="lax" />
</xs:complexType>
<xs:element name="Body" type="tns:Body" />
<xs:complexType name="Body" >
<xs:sequence>
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
</xs:sequence>
<xs:anyAttribute namespace="##any" processContents="lax" >
<xs:annotation>
<xs:documentation>
Prose in the spec does not specify that attributes are allowed on the Body element
</xs:documentation>
</xs:annotation>
</xs:anyAttribute>
</xs:complexType>
<!-- Global Attributes. The following attributes are intended to be usable via qualified attribute names on any complex type referencing them. -->
<xs:attribute name="mustUnderstand" >
<xs:simpleType>
<xs:restriction base='xs:boolean'>
<xs:pattern value='0|1' />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="actor" type="xs:anyURI" />
<xs:simpleType name="encodingStyle" >
<xs:annotation>
<xs:documentation>
'encodingStyle' indicates any canonicalization conventions followed in the contents of the containing element. For example, the value 'http://schemas.xmlsoap.org/soap/encoding/' indicates the pattern described in SOAP specification
</xs:documentation>
</xs:annotation>
<xs:list itemType="xs:anyURI" />
</xs:simpleType>
<xs:attribute name="encodingStyle" type="tns:encodingStyle" />
<xs:attributeGroup name="encodingStyle" >
<xs:attribute ref="tns:encodingStyle" />
</xs:attributeGroup>
<xs:element name="Fault" type="tns:Fault" />
<xs:complexType name="Fault" final="extension" >
<xs:annotation>
<xs:documentation>
Fault reporting structure
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="faultcode" type="xs:QName" />
<xs:element name="faultstring" type="xs:string" />
<xs:element name="faultactor" type="xs:anyURI" minOccurs="0" />
<xs:element name="detail" type="tns:detail" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="detail">
<xs:sequence>
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
</xs:sequence>
<xs:anyAttribute namespace="##any" processContents="lax" />
</xs:complexType>
</xs:schema>
I reckon I've done something noddy.
Thanks in advance
The prefix "tns" is bound to different namespaces in the source and in the schema. So this is not a schema for the vocabulary of your source document.
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
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;
}
i have created basic xsd successfully however I want to add restriction for the element that it should be present and contains atleast one character. it also has 4 attributes. i am facing problem in adding restriction since I can not use simple type as the element has attributes.
Please suggest something
thanks in advance
Added XSD data posted by OP in comments (sic)
<xs:element name="Engines">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="Engine" />
</xs:sequence>
<xs:attribute name="Count" use="required" type="xs:integer" />
</xs:complexType>
</xs:element>
<xs:element name="Engine">
<xs:complexType>
<xs:sequence>
<xs:element name="Model" type="Model"/>
<xs:element ref="SerialNumber" />
</xs:sequence>
</xs:complexType>
</element>
<xs:simpleType name="trimValueType">
<xs:restriction base="xs:string">
<xs:minLength value="1"></xs:minLength>
<xs:whiteSpace value="collapse"></xs:whiteSpace>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="Model">
<xs:simpleContent>
<xs:extension base="trimValueType">
<xs:attribute name="ATTRIBUTE" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<Engines count = 1> <Engine> <Model ATTRIBUTE = "r\w"> </Model> <SerialNumber ATTRIBUTE = "r/w">1234567</SerialNumber> <Engine> <Engines>
You have to first create a simple type that restricts xsd:string to specify your text constraints. Then you need to define a complex type, with a simple content, which extends the simple type you just created using the attributes you want. I threw in a whitespace constraint, just to match your title, even though you're not specifically mention it in your problem statement.
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="one">
<xsd:restriction base="xsd:string">
<xsd:whiteSpace value="collapse"/>
<xsd:minLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="two">
<xsd:simpleContent>
<xsd:extension base="one">
<xsd:attribute name="one"/>
<xsd:attribute name="two"/>
<xsd:attribute name="etc"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:element name="root" type="two"/>
</xsd:schema>
Sample XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" one="anySimpleType" two="anySimpleType" etc="anySimpleType" xmlns="http://tempuri.org/XMLSchema.xsd">root1</root>
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>