针对XSD的XML验证:cvc-complex-type.2.4.a



目前我正在使用javax.xml.validation.Validator来验证给定xsd的xx。我已经设置了自定义错误处理程序来获取所有异常,而不是在第一个异常处退出。

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.

我怎样才能做到这一点?

作为第二个元素,xsd 指定 att2,放入您提供的att4
作为第三个元素,xsd 指定att3,但您没有提供任何元素。
您可以尝试将元素 att2 和/或att3设置为可选:

minOccurs="0" maxOccurs="1"

如果它不起作用,您可以尝试:

<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"/>

验证器作为有限状态机实现。它计算从一种状态到另一种状态的允许转换。读取att1元素后,唯一允许的过渡是att2,这就是它告诉你的。探索整个有限状态机并计算出如果有一个att2然后是一个att3,那么在att4是有效的,这还不够聪明。

撒克逊验证器比这好一点,但只有一点点:它不会给你你想要的东西。

如果有什么安慰的话,XSD验证器通常比正则表达式引擎做得更好(这本质上就是这样(;正则表达式引擎通常只是告诉你输入与正则表达式不匹配,并且不知道为什么。

相关内容

最新更新