Jaxb - Unable to revoke token from XSD that included other xsd
I have an XSD (copy-metainfo.xsd) that includes other XSDs (test-component-types.xsd). I already generated artifacts from copy-metainfo, but I am trying to unmarshall from copy-metainfo.xsd using the jaxb api.
here is my xsd.
test-component-types.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="testComponent">
<xs:attribute name="type" type="testComponentType"/>
</xs:complexType>
<xs:simpleType name="testComponentType">
<xs:restriction base="xs:string">
<xs:enumeration value="TYPE1"/>
<xs:enumeration value="TYPE2"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
copy-metainfo.xsd
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:test="http://xmlns.test.com/cie/test/copy-metainfo"
targetNamespace="http://xmlns.test.com/cie/test/copy-metainfo">
<xs:include schemaLocation="test-component-types.xsd"/>
<xs:element name="copy-metainfos" type="test:copy-metainfos-type" />
<xs:complexType name="copy-metainfos-type">
<xs:sequence>
<xs:element name="copy-metainfo" type="test:copy-metainfo" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="copy-metainfo">
<xs:sequence>
<xs:element name="file-paths" type="test:FilePaths" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="test-type" type="test:testComponentType"/>
</xs:complexType>
<xs:complexType name="FilePaths">
<xs:sequence>
<xs:element name="location" type="test:Location" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Location">
<xs:attribute name="src" type="xs:string"/>
</xs:complexType>
</xs:schema>
When I try to unmarshall from copy-metainfo.xsd file with the following code, I get an error. These XSDs are contained in one jar.
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{CopyMetaInfos.class});
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);
StreamSource xsdSource = new StreamSource(xsdStream);
Schema schema = schemaFactory.newSchema(xsdSource);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
return (CopyMetaInfos) unmarshaller.unmarshal(is);
Mistake:
org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 64; src-resolve: Unable to resolve name "test: testComponentType" to typedef component (n).
line 22 is ''.
This is the XML I'm trying to unbind from the jar:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<copy-metainfos xmlns="http://xmlns.test.com/cie/test/copy-metainfo">
<copy-metainfo test-type="TYPE1">
<file-paths>
<location src="common"/>
</file-paths>
</copy-metainfo>
</copy-metainfos>
I have verified that the XSDs are in the jar and that the COPY_METAINFO_SCHEMA variable is pointing to the correct location. If I specify testComponentType directly in copy-metainfo.xsd instead of include test-component-types.xsd then it works.
Is there something wrong with XSD or XML or Java code?
source to share
XML is correct (semantically and syntactically), I confirmed it with XMLSpear
You have to add all XSDs to schema validator..test-component-types.xsd and copy-metainfo.xsd
InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);
this way you only added one XSD and got no definition test:testComponentType
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{CopyMetaInfos.class});
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//Source copy-metainfo.xsd
InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);
StreamSource xsdSource = new StreamSource(xsdStream);
//Source test-component-types.xsd
InputStream xsdStreamTest = CopyMetaInfos.class.getClassLoader().getResourceAsStream(TEST_COMPONENT_TYPES);
StreamSource xsdSourceTest = new StreamSource(xsdStreamTest);
Schema schema = schemaFactory.newSchema(new StreamSource[]{xsdSource,xsdSourceTest});
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
return (CopyMetaInfos) unmarshaller.unmarshal(is);
source to share