英文:
XSD Validation Catching ALL Errors within each complexType elemnt - FAILS to catch all
问题
我的目标是在JAVA中捕获XSD验证中**每个"complexType"**的所有错误 - 但当XML在一个complexType元素下有多个错误时,它会失败。
我正在使用来自 - https://www.baeldung.com/java-validate-xml-xsd
的示例代码。
使用的XSD -
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="individual">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element name="zip" type="xs:positiveInteger" />
<xs:element name="city" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
使用带有错误的XML -
<?xml version="1.0" encoding="UTF-8" ?>
<individual>
<name>Baeldung</name>
<age></age> <!-- error1 -->
<salary></salary> <!-- error2 -->
<address>
<zip>00001</zip>
<state></state> <!-- error3 -->
<city>New York</city>
<country></country> <!-- error4 -->
</address>
</individual>
Java代码中包含了上述链接中的ErrorHandler实现。
public class XmlErrorHandler implements ErrorHandler ....
现在,正如我们所看到的,XML中有2个complexTypes下的4个新元素。验证列表仅报告来自complexTypes的每个一个错误。它无法在ErrorHandler实现中捕获所有错误。
从ErrorHandler实现中的列表 - 只有2个错误中的4个!!
cvc-complex-type.2.4.a: 找到了以元素 'age' 开始的无效内容。其中之一预期为'{address}'。
cvc-complex-type.2.4.a: 找到了以元素 'state' 开始的无效内容。其中之一预期为'{city}'。
有人可以帮助我吗?如何修复每个complexType中的所有错误?
谢谢。
英文:
My objective is to catch All error in XSD validation for each and every "complexType" in JAVA - But it is failing when xml has more than one error under a complexType element.
I am using the Example code from - https://www.baeldung.com/java-validate-xml-xsd
XSD used -
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="individual">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element name="zip" type="xs:positiveInteger" />
<xs:element name="city" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML used with errors introduced -
<?xml version="1.0" encoding="UTF-8" ?>
<individual>
<name>Baeldung</name>
<age></age> <!-- error1 -->
<salary></salary> <!-- error2 -->
<address>
<zip>00001</zip>
<state></state> <!-- error3 -->
<city>New York</city>
<country></country> <!-- error4 -->
</address>
</individual>
java code has the implementation of ErrorHandler from the above link .
public class XmlErrorHandler implements ErrorHandler ....
Now, as we see, in XML there are 4 new elements under 2 complexTypes. The Validation list reports only has one error each from complexTypes. It cannot catch all in ErrorHandler implementation.
list from Errorhandler implementation - Only 2 errors out of 4 !!
> cvc-complex-type.2.4.a: Invalid content was found starting with element 'age'. One of '{address}' is expected.
>
> cvc-complex-type.2.4.a: Invalid content was found starting with element 'state'. One of '{city}' is expected.
Can anyone help me? How to get all errors from each complexType fixed?
Thanks.
答案1
得分: 1
解析内容与语法相对比时恢复错误是一门困难的艺术。与解析 Java 相比。给定输入如下:
String s := "10" - 3f4 ;
有多少错误?也许“-”应该是“+”?也许“10”应该没有引号的情况下是10?也许“3f4”应该是“3e4”,或者也许它应该是“3+4”?这就像火柴棍谜题:从错误的排列到有效的排列需要移动的最少火柴棍数量是多少?您可以将这些移动中的每一个视为一个错误,并且您可以要求处理器报告将所写内容转换为有效内容所需的最少更改数量。但是解析器通常不会那么聪明,人们也不指望它们聪明。解析器报告它们看到的第一个不是有效继续的事物(在这里是“-”符号),然后它们会跳转到一个它们可以相对确定自己所在位置的地方 - 在 Java 中,通常会是分号。如果不这样做,您会冒着报告虚假错误的风险,因为您错误地猜测了第一个错误的真正性质,用户会因随后的错误毫无意义而感到沮丧。
英文:
Recovery from errors when parsing content against a grammar is a difficult art. Compare with parsing Java. Given input like
String s := "10" - 3f4 ;
how many errors are there? Perhaps "-" should have been "+"? Perhaps "10" should have been 10 without the quotes? Perhaps "3f4" should have been "3e4", or perhaps it should have been "3+4"? It's like the matchstick puzzles: what's the minimum number of matchsticks you have to move to get from a wrong arrangement to a valid arrangement? You can regard each of those moves as an error, and you could ask the processor to report the minimum number of changes needed to turn what's written into something that's valid. But parsers aren't usually that smart, and people don't expect them to be. Parsers report the first thing they see that isn't a valid continuation (here, the "-" sign), and then they skip forward to a point where they can be reasonably sure they know where they are - in Java, that will generally be the semicolon. If you don't do that, you risk reporting spurious errors because you've guessed wrong about the true nature of the first error, and users get frustrated because the subsequent errors make no sense.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论