Perl XML :: LibXML :: Schema将在第一次错误时停止验证

时间:2010-07-08 18:14:22

标签: xml perl xsd

我正在尝试使用XML :: LibXML :: Schema来针对xsd验证xml文件。问题是如果我的xml有多个语义问题,验证将在第一个上死亡而不报告其他问题。

It finds the first error, no reference to <foobar/> bash-3.2$ ./test.pl test.xsd test.xml xmlfile <test.xml> failed validation: test.xml:14: Schemas validity error : Element 'foobar': This element is not expected.

After I remove the first error, it finds another. "ten" is not an unsigned int xmlfile <test.xml> failed validation: test.xml:11: Schemas validity error : Element 'allocation': 'ten' is not a valid value of the atomic type 'xs:unsignedInt'.

Changing "ten" to 10 fixes this issue bash-3.2$ ./test.pl test.xsd test.xml No issues found

我想在一次运行中获得所有问题的报告。有任何想法吗?我应该使用另一个模块吗?遵循我的Perl脚本和我的xsd和xml文件。

感谢您的帮助,

圣保罗

<foobar/>

2 个答案:

答案 0 :(得分:2)

尝试将recover => 1传递给XML::LibXML->new。这可能有所帮助。

来自XML::LibXML::Parser的文档......

   recover
       /parser, html, reader/

       recover from errors; possible values are 0, 1, and 2

       A true value turns on recovery mode which allows one to parse
       broken XML or HTML data. The recovery mode allows the parser to
       return the successfully parsed portion of the input document. This
       is useful for almost well-formed documents, where for example a
       closing tag is missing somewhere. Still, XML::LibXML will only
       parse until the first fatal (non-recoverable) error occurs,
       reporting recoverable parsing errors as warnings. To suppress even
       these warnings, use recover=>2.

       Note that validation is switched off automatically in recovery
       mode.

答案 1 :(得分:1)

我已经与XML :: LibXML的维护者联系并填写了这个错误修复程序。事实证明,使用命令行实用程序xmllint:

可以报告多个错误

$ xmllint --schema test.xsd test.xml
<?xml version="1.0" encoding="UTF-8"?>
<request xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="test">
<channel name="channel">
<username>user</username>
<password>pass</password>
</channel>

<hotel id="ten">
<date from="2009-07-07" to="2009-07-17"/>
<room id="1">
<allocation>10</allocation>
</room>
</hotel>
<foobar/>
</request>
test.xml:8: element hotel: Schemas validity error : Element 'hotel', 
attribute 'id': 'ten' is not a valid value of 
the atomic type 'xs:unsignedInt'.
test.xml:14: element foobar: Schemas validity error : Element 'foobar': 
This element is not expected.
test.xml fails to validate