Spring boot 针对xsd的骆驼验证:找不到元素';地址';
我想对xsd进行骆驼验证,但出现了一个错误: 找不到元素“Address”的声明 我在较小的xml/xsd文件上遇到了这个问题 validation.xsd:Spring boot 针对xsd的骆驼验证:找不到元素';地址';,spring-boot,xsd,apache-camel,Spring Boot,Xsd,Apache Camel,我想对xsd进行骆驼验证,但出现了一个错误: 找不到元素“Address”的声明 我在较小的xml/xsd文件上遇到了这个问题 validation.xsd: <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="Address"> <xs:sequence>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="Street" type="xs:string" />
<xs:element name="HouseNo" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
错误是:
org.apache.camel.support.processor.validation.SchemaValidationException:
验证失败,原因是:
com.sun.org.apache.xerces.internal.jaxp.validation。SimpleXMLSchema@79c47167
错误:[org.xml.sax.SAXParseException:cvc elt.1.a:找不到
元素“地址”的声明,第2行,第10列
地址DTO类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Address")
@ToString
public class Address {
@XmlElement(name = "Street", required = true)
protected String street;
@XmlElement(name = "HouseNo", required = true)
protected String houseNo;
// getters, setters
}
错误是正确的,因为根据您的架构,您的XML无效。您声明了一个复杂类型
地址
,但验证正在查找一个元素地址
按如下方式修复您的架构:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="Street" type="xs:string"/>
<xs:element name="HouseNo" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
或者,我可以在底部添加
(在复杂类型定义旁边)。不确定哪一个更好。如果要重用复杂类型,最好将复杂类型与元素分开,甚至需要这样做。如果您知道复杂类型是唯一的,因此只使用一次,则如果您将其定义为嵌入元素中,则可以将其设为私有类型或封装类型。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Address")
@ToString
public class Address {
@XmlElement(name = "Street", required = true)
protected String street;
@XmlElement(name = "HouseNo", required = true)
protected String houseNo;
// getters, setters
}
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="Street" type="xs:string"/>
<xs:element name="HouseNo" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>