gpt4 book ai didi

jaxb - 无法从包含其他 xsd 的 XSD 解码

转载 作者:行者123 更新时间:2023-12-05 00:35:17 26 4
gpt4 key购买 nike

我有一个包含其他 XSD (test-component-types.xsd) 的 XSD(copy-metainfo.xsd)。我已经从 copy-metainfo 生成了工件,但我正在尝试使用 jaxb api 从 copy-metainfo.xsd 解码。

这是我的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>

当我尝试使用以下代码从 copy-metainfo.xsd 解码时,出现错误。这些 XSD 包含在同一个 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);

错误:

org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 64; src-resolve: Cannot resolve the name 'test:testComponentType' to a(n) 'type definition' component.

第 22 行是 ''。

这是我试图从 jar 中解码的 XML:

    <?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>

我验证了 XSD 在 jar 中并且“COPY_METAINFO_SCHEMA”变量指向正确的位置。如果我直接在 copy-metainfo.xsd 中指定 testComponentType 而不是包含 test-component-types.xsd,那么它就可以工作。

XSD 或 XML 或 java 代码有什么问题吗?

最佳答案

XML 是正确的(在语义和句法上),我通过 XMLSpear 验证了它

您必须将所有 XSD 添加到模式验证器 ..test-component-types.xsd 和 copy-metainfo.xsd

InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);

以这种方式,您只添加了一个 XSD 并且缺少 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);

关于jaxb - 无法从包含其他 xsd 的 XSD 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26068773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com