简体   繁体   English

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

[英]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.

相关问题 XSD 错误:cvc-complex-type.2.4.a:发现无效内容以元素开头 - XSD error: cvc-complex-type.2.4.a: Invalid Content Was Found Starting With Element RAD中的Websphere Liberty server.xml在密钥库上提供了cvc-complex-type.2.4.a - Websphere Liberty server.xml in RAD gives cvc-complex-type.2.4.a on keystore cvc-complex-type.2.4。在faces-config.xml中发现了以元素工厂开头的无效内容 - cvc-complex-type.2.4.a invalid content was found starting with element factory in faces-config.xml (xml)cvc-complex-type.2.4.a:发现无效内容,以元素“ init-param”开头 - (xml) cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param' JAXB验证错误-cvc-complex-type.2.4.a:发现无效的内容(从元素&#39;codeSystem&#39;开始)。 预期为“ {codeSystem}”之一 - JAXB Validation error - cvc-complex-type.2.4.a: Invalid content was found starting with element 'codeSystem'. One of '{codeSystem}' is expected weblogic.xml出错:cvc-complex-type.2.4.a:从元素'prefer-application-packages'开始发现无效内容 - Error in weblogic.xml : cvc-complex-type.2.4.a: Invalid content was found starting with element 'prefer-application-packages' web.xml 中的错误:cvc-complex-type.2.4.a:发现以元素“context-root”开头的无效内容 - Error in web.xml: cvc-complex-type.2.4.a: Invalid content was found starting with element 'context-root' cvc-complex-type.2.4.a:发现无效的内容(从元素“ beans”开始) - cvc-complex-type.2.4.a: Invalid content was found starting with element 'beans' cvc-complex-type.2.4.a:从元素Media开头发现无效的内容 - cvc-complex-type.2.4.a: Invalid content was found starting with element Media cvc-complex-type.2.4.a:发现无效内容,以元素&#39;beans:bean&#39;开头 - cvc-complex-type.2.4.a: Invalid content was found starting with element 'beans:bean'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM