提问人:lapin 提问时间:11/17/2023 最后编辑:lapin 更新时间:11/17/2023 访问量:35
JAXB 的 XML 模式验证问题
XML schema validation issue with JAXB
问:
我制作了一个 XSD,并有一个 XML 模式来使用 JAXB 进行验证。验证失败。我不明白,我不知道如何解决这个问题。你能帮帮我吗?提前致谢。
XSD:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://localhost/universe/menu-schema.xsd"
targetNamespace="http://localhost/universe/menu-schema.xsd">
<xs:complexType name="menuType">
<xs:sequence>
<xs:element name="titre" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="lien" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="action" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="menus" type="tns:menusType" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="menusType">
<xs:sequence>
<xs:element name="menu" type="tns:menuType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="menus" type="tns:menusType"/>
</xs:schema>
XML格式:
<?xml version="1.0" encoding="UTF-8"?>
<xs:menus
xmlns:xs="http://localhost/universe/menu-schema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:menu>
<xs:titre>Accueil</xs:titre>
<xs:lien>accueil</xs:lien>
</xs:menu>
<xs:menu>
<xs:titre>Guide des épisodes</xs:titre>
<xs:lien>guideDesEpisodes</xs:lien>
<xs:menus>
<xs:menu>
<xs:titre>Introduction</xs:titre>
<xs:lien>introduction</xs:lien>
</xs:menu>
<xs:menu>
<xs:titre>La série</xs:titre>
<xs:lien>laSerie</xs:lien>
</xs:menu>
</xs:menus>
</xs:menu>
<xs:menu>
<xs:titre>Forum</xs:titre>
<xs:lien>http://le-multivers.forumpro.fr/c14-farscape</xs:lien>
</xs:menu>
</xs:menus>
JAXB 解析:解组时失败
JAXBContext jaxbContext = JAXBContext.newInstance(MenusType.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(xsdFile);
jaxbUnmarshaller.setSchema(schema);
return (MenusType) jaxbUnmarshaller.unmarshal(xmlFile);
先谢谢你
jakarta.xml.bind.UnmarshalException: élément inattendu (URI : "http://localhost/universe/menu-schema.xsd", local : "menus"). Les éléments attendus sont (none) at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:693) at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportError(Loader.java:230) at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportError(Loader.java:225) at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:92) at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1102) at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:527) at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:509) at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:75) at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137) at
已使用的 XSD 和 XML 资源。我尝试使用 xsd 架构验证 xml,但失败了。我不知道为什么。Shema 似乎设置正确(?
答:
0赞
Michael Kay
11/17/2023
#1
您的架构有效,但实例文档对架构无效。以下是撒克逊验证者不得不说的话:
Validation error on line 5 column 13 of test.xml:
FORG0001: In content of element <xs:menus>: The content model does not allow element
<xs:menu> to appear as the first child. The element is in namespace
http://localhost/universe/menu-schema.xsd but it should be in no namespace.
See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 13 column 19 of test.xml:
FORG0001: In content of element <xs:menus>: The content model does not allow element
<xs:menu> to appear as the first child. The element is in namespace
http://localhost/universe/menu-schema.xsd but it should be in no namespace.
See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Error on line 27 column 12 of test.xml:
XQDY0027 Two validation errors were reported. First error: In content of element
<xs:menus>: The content model does not allow element <xs:menu> to appear as the first
child. The element is in namespace http://localhost/universe/menu-schema.xsd but it should
be in no namespace.
Validation complete: errors found
如果要更改架构以便实例文档进行验证,请添加到元素中。或者,如果要更改实例文档,使其根据所写入的架构进行验证,请更改元素,例如 和,以便它们不位于命名空间中。elementFormDefault="qualified"
xs:schema
titre
lien
评论
0赞
lapin
11/17/2023
感谢您的回复。它已通过以这种方式更改 xsd 来生成 xml 根元素注释来修复(根元素没有外部复杂类型)
评论