Unmarshalling complex xml having nested child elements - java

I want to unmarshal a given xml file using jaxb2.
Here is the source xml document.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<calendarList>
<calendar>
<calendarCode>Default</calendarCode>
<weeklyDefault>1111111</weeklyDefault>
<exceptionList>
<exception>
<exceptionDate>2012-03-01T00:00:00</exceptionDate>
<isOpen>false</isOpen>
</exception>
<exception>
<exceptionDate>2012-03-02T00:00:00</exceptionDate>
<isOpen>false</isOpen>
</exception>
</exceptionList>
</calendar>
<calendar/>
<calendarList>
</root>
for this I defined following xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.0">
<xsd:element name="root" type="Root" />
<xsd:complexType name="Root">
<xsd:sequence>
<xsd:element name="calendarList" type="CalendarList" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="CalendarList">
<xsd:sequence>
<xsd:element name="calendar" type="Calendar" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Calendar">
<xsd:sequence>
<xsd:element name="calendarCode" type="xsd:string" />
<xsd:element name="weeklyDefault" type="xsd:string" />
<xsd:element name="exceptionList" type="ExceptionList" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ExceptionList">
<xsd:sequence>
<xsd:element name="exceptionCalendar" type="ExceptionCalendar" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ExceptionCalendar">
<xsd:sequence>
<xsd:element name="exceptionDate" type="xsd:dateTime" />
<xsd:element name="isOpen" type="xsd:boolean"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Using JAXB I generated the classes for this but when I am unmarshalling I only able to get the Calendar Objects but not the nested "Exception" objects inside Calendar's ExceptionList.
Following code will explain above
public void CheckResults(filePath){
Root ods = handler.unmarshal(filePath);
for(Calendar calendar : ods.getCalendarList().getCalendar())
{
System.out.println(calendar.getCalendaeCode()); //Here I have the element calendar
//but calendar.getExceptionList().getExceptionCalendar() has no member
for (ExceptionCalendar expCal : calendar.getExceptionList().getExceptionCalendar())
{
System.out.println(expCal.getExceptionDate());
}
}
}
Here is the logic for handler.unmarshal method
public Root unmarshal(String filePath) {
try{
JAXBContext jc = JAXBContext.newInstance(DOMAIN_PKG);
Unmarshaller unmarsaller = jc.createUnmarshaller();
JAXBElement<Root> oDS;
if(filePath.isEmpty()) {
oDS = (JAXBElement<Root>) unmarsaller.unmarshal(System.in);
} else {
File file = new File(filePath);
oDS = (JAXBElement<Root>) unmarsaller.unmarshal(file);
}
return oDS.getValue();
}catch(JAXBException exp){
exp.printStackTrace();
}
return null;
}
It would be a great help if someone can explain how the object creation takes place while unmarshalling. Probably I am missing something small but important here.

I think your schema is wrong, replace name="ExceptionCalendar" by name="exception" and regenerate JAXB objects.
<xsd:complexType name="ExceptionList">
<xsd:sequence>
<xsd:element name="exception" type="ExceptionCalendar" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ExceptionCalendar">
<xsd:sequence>
<xsd:element name="exceptionDate" type="xsd:dateTime" />
<xsd:element name="isOpen" type="xsd:boolean"/>
</xsd:sequence>
</xsd:complexType>

Related

cvc-elt.1: Cannot find the declaration of element 'change-set'

I'm trying to use some open-source software for RFID services (GitHub: fosstrak capturing application) from a few years ago and there's this error that I can't fix.
(null: 3, 230): cvc-elt.1: Cannot find the declaration of element 'change-set'.
I'm using Docker to containerise the application running Tomcat7 with Java8.
The XML looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<change-set xmlns="http://drools.org/drools-5.0/change-set"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://drools.org/drools-5.0/change-set.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd">
<add>
<resource source="classpath:drools/SimpleEPCISDocument.drl" type="DRL" />
</add>
</change-set>
And the XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://drools.org/drools-5.0/change-set"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xmlns:change-set="http://drools.org/drools-5.0/change-set">
<xsd:import namespace="http://www.w3.org/2001/XMLSchema-instance"/>
<xsd:element name="change-set">
<xsd:complexType>
<xsd:choice>
<xsd:element ref="change-set:add"/>
<xsd:element ref="change-set:remove"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<xsd:element name="add">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" ref="change-set:resource"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="remove">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" ref="change-set:resource"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="resource">
<xsd:complexType>
<xsd:sequence>
<xsd:any minOccurs="0"/>
<xsd:element minOccurs="0" ref="change-set:decisiontable-conf"/>
</xsd:sequence>
<!-- URL to the resource, can be file based -->
<xsd:attribute name="source" use="required" type="xsd:anyURI"/>
<!-- for example, DRL, or PKG -->
<xsd:attribute name="type" use="required" type="xsd:string"/>
<xsd:attribute name="basicAuthentication" type="xsd:string"/>
<xsd:attribute name="username" type="xsd:string"/>
<xsd:attribute name="password" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="decisiontable-conf">
<xsd:complexType>
<xsd:attribute name="input-type" use="required" type="xsd:NCName"/>
<xsd:attribute name="worksheet-name" use="required" type="xsd:NCName"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Change
xs:schemaLocation="http://drools.org/drools-5.0/change-set.xsd [...]
to
xs:schemaLocation="http://drools.org/drools-5.0/change-set [...]
because schemaLocation must be a series of pairs of namespace URIs and XSD locations. In this case, the namespace URI is http://drools.org/drools-5.0/change-set, which matches the namespace of the root element in your XML and the target namespace of your XSD.
See also
How to reference a local XML Schema file correctly?
You'll also have to deal with a subsequent Unique Particle Attribute issue, but that's a separate matter deserving its own question if you're unable to resolve. Start with XSD validation - ANY in SEQUENCE ("Unique Particle Attribution").

'OR' operation on element type in XSD

This is a continuity of My question
Assume below is my xsd file,
Updated XSD file
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/wm" xmlns="http://www.example.com/wm" elementFormDefault="qualified" version="1.0">
<xsd:include schemaLocation="Comm.xsd" />
<xsd:element name="testEame1">
<xsd:annotation>
<xsd:documentation> test </xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="id" type="xsd:string" minOccurs="1" />
<xsd:element name="session" type="rawSess" minOccurs="1" />
</xsd:sequence>
<xsd:attribute name="pid" type="xsd:integer" use="required" />
<xsd:attribute name="version" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
references another XSD which has type defined,
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" targetNamespace="http://www.example.com/wm" xmlns="http://www.example.com/wm" elementFormDefault="qualified">
<xsd:complexType name="rawSess">
<xsd:sequence>
<xsd:element name="oldSessionVersion" type="Sessiontype1" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="sessInfo">
<xsd:sequence>
<xsd:element name="newSessionVersion" type="Sessiontype2" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Sessiontype1">
<xsd:sequence>
<xsd:element name="ele1" type="xsd:string" minOccurs="0" />
<xsd:element name="ele2" type="xsd:string" />
<xsd:element name="ele3" type="xsd:string" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Sessiontype2">
<xsd:sequence>
<xsd:element name="nele1" type="xsd:integer" minOccurs="0" />
<xsd:element name="nele2" type="xsd:integer" />
<xsd:element name="nele3" type="xsd:integer" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
In the above xml, for element session , I want it to have only two types like below. Either sessInfo or rawSess
<xsd:element name="session" type='sessInfo| rawSess' minOccurs="1"/>
Update Note : userDefinedtypes are complex types.
How can I configure my xsd to achieve this
If userDefinedType1 and userDefinedType2 are simple atomic types, then define a union type:
<xs:element name="session" type="one-or-two"/>
<xs:simpleType name="one-or-two">
<xs:union memberTypes="userDefinedType1 userDefinedType2"/>
</xs:simpleType>
We now know that they are actually complex types. Define a type
<xs:complexType name="eitherType">
<xs:choice>
<xs:sequence>... content model of alternative 1</xs:sequence>
<xs:sequence>... content model of alternative 2</xs:sequence>
</xs:choice>
</xs:complexType>
and reference this type from the element declaration.
NB: this only works if there is no ambiguity, that is, if the validator can work out which branch of the choice to take based on the first element name that it sees in the instance document. If your choice were between two content models that both start with an h1 element, for example, you would have to reorganise the content models to remove the ambiguity.

validate parts of the incoming web service request lazily in JAX-WS RI

I have to process big requests in a Java 8 web service (JAX-WS RI). A request contains a "header" and many "records" like this (not that 2nd, 3rd, 4th, 5th student records are invalid according to the schema):
<helloStudentsServiceRequest>
<workshop>
<name>wsname</name>
<tutor>tutorname</tutor>
</workshop>
<studentList>
<!--1 or more repetitions: -->
<student>
<Name>st1</Name>
<Birth>1999-11-11</Birth>
</student>
<student>
<Name>st2</Name>
</student>
<student>
<Name>st2</Name>
<Birth>199O-11-11</Birth>
</student>
<student>
<Name>st3</Name>
<Birth>stoneage</Birth>
</student>
<student>
<Birth>stoneage</Birth>
</student>
<student>
<Name>st6</Name>
<Birth>2001-11-12</Birth>
</student>
</studentList>
</helloStudentsServiceRequest>
I use a schema (XSD) for describing my interface. For example:
<xsd:complexType name="HelloStudentsServiceRequestType">
<xsd:sequence>
<xsd:element name="workshop" type="WorkshopType" />
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" type="StudentType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="WorkshopType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="tutor" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="StudentType">
</xsd:annotation>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string" />
<xsd:element name="Birth" type="xsd:date" />
</xsd:sequence>
</xsd:complexType>
I'd like to use this schema for validating the request.
However I am not allowed to reject a request if a few "record" in it is not valid according to the schema. In that case I have to process the valid records and return some meaningful information (preferably the SAX error) regarding the invalid ones.
I'd like to keep the request's schema free from the technical details neccessary to fullfill this "lazy validation". For example I don't want to use a "big string" to hold the "record" and then validate that string against the schema. That would break the interface and would be difficult to communicate to the clients. So this is no-go for me:
...
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" type="xsd:string" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
...
Do you know an elegant way to solve this in JAX-WS RI (JAXB2) ?
It's possible to customize JAXB bindings to map part of the XML to DOM. For details:
https://jaxb.java.net/guide/Avoid_strong_databinding.html#Mapping_to_DOM
So I mapped "WorkshopType" and "StudentType" to DOM. Now it's possible to validate those DOMs individually and reject or partially process the incoming message. It's also possible to catch the SAX exception if a DOM validating is not successful and signal that back to the client.
working example zipped
I used this binding customization:
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<jxb:bindings
schemaLocation="WorkshopType.xsd"
node="//xsd:complexType[#name='WorkshopType']">
<jxb:dom />
</jxb:bindings>
<jxb:bindings
schemaLocation="StudentType.xsd"
node="//xsd:complexType[#name='StudentType']">
<jxb:dom />
</jxb:bindings>
And these are the schemas:
For the request:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" xmlns="helloStudentsNS"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
targetNamespace="helloStudentsNS">
<xsd:include schemaLocation="StudentType.xsd" />
<xsd:include schemaLocation="WorkshopType.xsd" />
<xsd:element name="helloStudentsServiceRequest" type="HelloStudentsServiceRequestType" />
<xsd:element name="helloStudentsServiceResponse" type="HelloStudentsServiceResponseType" />
<xsd:complexType name="HelloStudentsServiceRequestType">
<xsd:sequence>
<xsd:element name="workshop" type="WorkshopType" />
<xsd:element name="studentList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" type="StudentType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="HelloStudentsServiceResponseType">
<xsd:sequence>
<xsd:element name="GreetingHeader" type="xsd:string" />
<xsd:element name="StudentGreeting" type="xsd:string" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
For the workshop and student types:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns="helloStudentsNS"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0"
targetNamespace="helloStudentsNS">
<xsd:element name="workshop" type="WorkshopType"/>
<xsd:complexType name="WorkshopType">
<!-- <xsd:annotation><xsd:appinfo><jaxb:dom/></xsd:appinfo></xsd:annotation> -->
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="tutor" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns="helloStudentsNS"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0" targetNamespace="helloStudentsNS">
<!--
-->
<xsd:element name="student" type="StudentType"/>
<xsd:complexType name="StudentType">
<!-- <xsd:annotation><xsd:appinfo><jaxb:dom/></xsd:appinfo></xsd:annotation> -->
<xsd:sequence>
<xsd:element name="Name" type="xsd:string" />
<xsd:element name="Birth" type="xsd:date" />
</xsd:sequence>
</xsd:complexType>
And this is the java code:
public HelloStudentsServiceResponseType greetStudents(HelloStudentsServiceRequestType req) {
WorkshopType ws;
// unmarshall workshop data
Element element = req.getWorkshop(); // workshop is retrieved as DOM
try {
Unmarshaller u = jc.createUnmarshaller(); // jaxb ri unmarshaller not thread safe: https://jaxb.java.net/guide/Performance_and_thread_safety.html
u.setSchema(workshopSchema); // enable validation by workshopType.xsd
ws = u.unmarshal(element, WorkshopType.class).getValue(); // unmarshal
} catch (JAXBException e) {
// this is fatal
throw new RuntimeException("fatal: invalid workshop data: ", e);
}
// unmarshall students
List<String> studentGreetings = new ArrayList<>();
for(Element studentElement : req.getStudentList().getStudent()) { // retrieve students as DOMs
try {
StudentType st;
Unmarshaller u = jc.createUnmarshaller(); // jaxb ri unmarshaller not thread safe: https://jaxb.java.net/guide/Performance_and_thread_safety.html
u.setSchema(studentSchema); // enable validation by studentType.xsd
st = u.unmarshal(studentElement, StudentType.class).getValue();
studentGreetings.add("Dear " + st.getName() + ", " + st.getBirth() + ", welcome in the workshop!");
} catch (JAXBException e) {
Throwable cause = e.getLinkedException();
if(cause != null) {
studentGreetings.add("Dear student, your data is invalid: " + cause.getMessage());
}
else {
studentGreetings.add("Dear student, there's an unknown problem with your data");
}
}
}
// construct response
HelloStudentsServiceResponseType response = new HelloStudentsServiceResponseType();
response.setGreetingHeader("I'd like to greet everybody in the " + ws.getName() + " workshop. " +
"Your tutor is " + ws.getTutor());
response.getStudentGreeting().addAll(studentGreetings);
return response;
}

Restrict value of an XML attribute to different attribute's/element's value

I will start with an example
<template>
<components>
<component name="switch" />
<component name="server" />
</components>
<layout>
<grid>
<position componentName="switch" positionX="0" positionY="0" />
</grid>
</layout>
</template>
What I want is to restrict values in componentName attribute to match one of the names specified above in components. Is this possible in JAXB? Because I need to have annotated classes which are then used to generate XSD.
Given your scenario, XSD 1.0 can enforce your "referential integrity" through a key/keyref combo. However, I am not aware of what annotations there are for these constructs in JAXB (it sounds as if you're looking at generating an XSD from you Java classes); at least I never ran into such annotations (see the list here)
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="template">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="components">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="component">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="layout">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="grid">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="position">
<xsd:complexType>
<xsd:attribute name="componentName" type="xsd:string" use="required" />
<xsd:attribute name="positionX" type="xsd:unsignedByte" use="required" />
<xsd:attribute name="positionY" type="xsd:unsignedByte" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="ComponentsKey">
<xsd:selector xpath="components/component"/>
<xsd:field xpath="#name"/>
</xsd:key>
<xsd:keyref name="MatchComponent" refer="ComponentsKey">
<xsd:selector xpath="layout/grid/position"/>
<xsd:field xpath="#componentName"/>
</xsd:keyref>
</xsd:element>
</xsd:schema>

cvc-elt.1: Cannot find the declaration of element 'staff' - Cannot validate xml against a xsd

I can't validate xml document against xsd schema.
And I couldn't figure out what is wrong there.
Here is xml file:
<?xml version="1.0" encoding="UTF-8"?>
<staff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employee.xsd">
<employee>
<name>Carl Cracker</name>
<salary>75000</salary>
<hiredate year="1987" month="12" day="15" />
</employee>
<employee>
<name>Harry Hacker</name>
<salary>50000</salary>
<hiredate year="1989" month="10" day="1" />
</employee>
<employee>
<name>Tony Tester</name>
<salary>40000</salary>
<hiredate year="1990" month="3" day="15" />
</employee>
</staff>
And xsd file:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="staff">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="employee" maxOccurs="unbounded" minOccurs="0">
<xsd:complexType>
<xsd:sequence>
<xsd:element type="xsd:string" name="name"/>
<xsd:element type="xsd:int" name="salary"/>
<xsd:element name="hiredate">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute type="xsd:short" name="year" use="optional"/>
<xsd:attribute type="xsd:byte" name="month" use="optional"/>
<xsd:attribute type="xsd:byte" name="day" use="optional"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
And output is next:
cvc-elt.1: Cannot find the declaration of element 'staff'.
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
at com.lab.edu.DOMTreeParser.parseStaff(DOMTreeParser.java:77)
at com.lab.edu.DOMTreeParser.<init>(DOMTreeParser.java:42)
at com.lab.edu.Application.main(Application.java:7)
Here is snippet of code:
public DOMTreeParser(String filename) {
employeeList = new ArrayList<>();
boolean result = validate(filename);
System.out.println("it is validate result: " + result);
if (result == false) {
System.out.println("XML isn't valid");
System.exit(0);
}
// if all are correct - parse xml
parseStaff(document.getDocumentElement());
}
private boolean validate(String filename) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setIgnoringElementContentWhitespace(true);
builder = factory.newDocumentBuilder();
builder.setErrorHandler(new SimpleErrorHandler());
document = builder.parse(new File(filename));
return true;
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
return false;
}
}
I tried to validate xml accord xsd at validator - all was valid.
What is cause of this cvs.. message?
Any suggestions?
The outermost element of your instance document must match a global element declaration in the schema. You only have one global element declaration, for the "staff" element, which is not present in your instance. If you want the "employee" element to validate, you will have to promote it from a local element declaration to a global one, which means rewriting the schema as:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="staff">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="employee" maxOccurs="unbounded" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="employee">
<xsd:complexType>
<xsd:sequence>
<xsd:element type="xsd:string" name="name"/>
<xsd:element type="xsd:int" name="salary"/>
<xsd:element name="hiredate">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute type="xsd:short" name="year" use="optional"/>
<xsd:attribute type="xsd:byte" name="month" use="optional"/>
<xsd:attribute type="xsd:byte" name="day" use="optional"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Categories