[英]XML Validation against XSD : cvc-complex-type.2.4.a
Currently I am using javax.xml.validation.Validator to validate xml against given xsd. 目前,我正在使用javax.xml.validation.Validator针对给定的xsd验证xml。 I have set custom error handler to get all exceptions instead of exiting at the first exception.
我已将自定义错误处理程序设置为获取所有异常,而不是在第一个异常退出。
Sample xsd:
<xs:element type="xs:string" name="att1"/>
<xs:element type="xs:string" name="att2"/>
<xs:element type="xs:string" name="att3"/>
<xs:element type="xs:string" name="att4"/>
In xml if att2 and att3 values are not there, I am getting below exception.
cvc-complex-type.2.4.a: Invalid content was found starting with element 'att4'. One of '{"https://******":att2}' is expected.
But I need exception to be like this i.e. both att2 and att3 should be shown in expected list.
cvc-complex-type.2.4.a: Invalid content was found starting with element 'att4'. One of '{"https://******":att2, "https://******":att3}' is expected.
How can I achieve this? 我该如何实现?
As a second element, the xsd specifies att2 , put you provided att4 . 作为第二个元素,xsd指定att2 ,您提供了att4 。
As a third element, the xsd specifies att3 but you didn't provide any element. 作为第三个元素,xsd指定att3,但是您未提供任何元素。
You can try to set the elements att2 and/or att3 as optional with : 您可以尝试使用以下命令将元素att2和/或att3设置为可选:
minOccurs="0" maxOccurs="1"
If it doesn't work, you can try : 如果不起作用,可以尝试:
<xs:element type="xs:string" name="att1"/>
<xs:choice minOccurs="0" maxOccurs="2">
<xs:sequence>
<xs:element maxOccurs="1" name="att2" type="xs:string" />
<xs:element maxOccurs="1" name="att3" type="xs:string" />
</xs:sequence>
</xs:choice>
<xs:element type="xs:string" name="att4"/>
The validator is implemented as a finite state machine. 验证器实现为有限状态机。 It computes the permitted transitions from one state to another.
它计算从一种状态到另一种状态的允许转变。 After reading an
att1
element, the only permitted transition is to an att2
, and that's what it's telling you. 读取后
att1
元素,唯一的允许过渡到att2
,而这正是它告诉你。 It's not smart enough to explore the entire finite state machine and work out that at att4
would be valid if there was an att2
and then an att3
. 这是不够聪明,探索整个有限状态机,并制定出在
att4
将是有效的,如果有一个att2
,然后一个att3
。
The Saxon validator does a little better than this, but only a little: it won't give you what you're looking for here. Saxon验证器的功能要比这好一点,但只有一点:它不会为您提供您在这里寻找的东西。
If it's any consolation, XSD validators generally do a lot better than regex engines (which is essentially what they are); 如果可以的话,XSD验证器通常比正则表达式引擎要好得多(本质上就是它们)。 regex engines generally just tell you that the input isn't matched by the regex, and give you no clue why.
正则表达式引擎通常只会告诉您输入与正则表达式不匹配,并且不提供任何提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.