![](/img/trans.png)
[英]XSD error: cvc-complex-type.2.4.a: Invalid Content Was Found Starting With Element
[英]XML Validation against XSD : cvc-complex-type.2.4.a
目前,我正在使用javax.xml.validation.Validator針對給定的xsd驗證xml。 我已將自定義錯誤處理程序設置為獲取所有異常,而不是在第一個異常退出。
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
,而這正是它告訴你。 這是不夠聰明,探索整個有限狀態機,並制定出在att4
將是有效的,如果有一個att2
,然后一個att3
。
Saxon驗證器的功能要比這好一點,但只有一點:它不會為您提供您在這里尋找的東西。
如果可以的話,XSD驗證器通常比正則表達式引擎要好得多(本質上就是它們)。 正則表達式引擎通常只會告訴您輸入與正則表達式不匹配,並且不提供任何提示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.