我想对xsd进行骆驼验证,但出现错误:
找不到元素“地址”的声明
我在较小的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>
<xs:element name="Street" type="xs:string" />
<xs:element name="HouseNo" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
xml问题:
<?xml version="1.0" encoding="UTF-8"?>
<Address>
<Street>string</Street>
<HouseNo>string</HouseNo>
</Address>
骆驼配置:
@Component
public class CamelRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration().component("servlet") // configure we want to use servlet as the component for the rest DSL
.bindingMode(RestBindingMode.json_xml) // enable json/xml binding mode
.dataFormatProperty("prettyPrint", "true") // output using pretty print
.contextPath("/c/api/")
.apiContextPath("/api-doc") // enable swagger api
.apiProperty("api.version", "2.0.0")
.apiProperty("api.title", "I")
.apiProperty("api.description", "I")
.apiProperty("api.contact.name", "A")
.apiProperty("cors", "true"); // enable CORS
// error handling to return custom HTTP status codes for the various exceptions
onException(StartProcessException.class)
.handled(true)
// use HTTP status 400 when input data is invalid
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
.setBody(simple("Invalid input data"));
rest()
.description("I")
.consumes("application/xml").produces("application/xml")
.post("/start").type(Address.class)
.bindingMode(RestBindingMode.json_xml).description("S")
.route().routeId("I").log("Message send: \n ${body}")
.to("validator:file:src/main/resources/validation.xsd")
.endRest();
}
}
错误是:
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
}
答案 0 :(得分:1)
该错误是正确的,因为根据您的模式,您的XML无效。您已声明复杂类型 Address
,但是验证正在寻找 Element Address
。
像这样修复您的架构:
<?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>