Perl XML::LibXML::Schema 将在第一个错误时停止验证 [英] Perl XML::LibXML::Schema will stop validation on first error

查看:28
本文介绍了Perl XML::LibXML::Schema 将在第一个错误时停止验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use the XML::LibXML::Schema to validate a xml file against a xsd. The problem is if my xml has multiple semantic issues the validation will die on the first one and not report the others.

它找到第一个错误,没有引用bash-3.2$ ./test.pl test.xsd test.xmlxmlfile <test.xml> 验证失败:test.xml:14:架构有效性错误:元素foobar":不需要此元素.

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.

在我删除第一个错误后,它发现了另一个错误.十"不是无符号整数xmlfile <test.xml> 验证失败:test.xml:11:架构有效性错误:元素分配":十"不是原子类型xs:unsignedInt"的有效值.

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'.

将十"更改为 10 可解决此问题bash-3.2$ ./test.pl test.xsd test.xml未发现问题

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

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

I would like to get a report of ALL the issues on a single run. Any ideas? Should I be using another module for this? Follows my Perl script and my xsd and xml files.

感谢您的帮助,

保罗

xsd:

 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <!-- channel -->
  <xsd:element name="channel">
   <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="username" type="xsd:string"/>
      <xsd:element name="password" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="name" use="required" type="xsd:string" />
   </xsd:complexType>
  </xsd:element>

 <!-- hotel -->
  <xsd:element name="hotel">
    <xsd:complexType>
      <xsd:sequence>
      <xsd:element name="date">
      <xsd:complexType>
        <xsd:attribute name="from" use="required" type="xsd:string" />
        <xsd:attribute name="to" use="required" type="xsd:string" />
      </xsd:complexType>
      </xsd:element>
      <xsd:element ref="room" minOccurs="1"/>
      </xsd:sequence>
      <xsd:attribute name="id" use="required" type="xsd:unsignedInt" />
    </xsd:complexType>
   </xsd:element>


 <!-- room -->
 <xsd:element name="room">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="allocation" type="xsd:unsignedInt"></xsd:element>
    </xsd:sequence>
    <xsd:attribute name="id" use="required" type="xsd:string" />
  </xsd:complexType>
 </xsd:element>

 <!-- building all together -->
 <xsd:element name="request">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="channel" maxOccurs="1"/>
      <xsd:element ref="hotel" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="type" use="required" type="xsd:string" />
  </xsd:complexType>
 </xsd:element>

xml:

<?xml version="1.0" encoding="UTF-8"?>
<request type="test" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <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>ten</allocation>
    </room>
  </hotel>
</request>

perl 脚本:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
#use Carp;
use vars qw( $xsd $xml $schema $parser $doc );

use XML::LibXML;


#
# MAIN
#
my $xsd = $ARGV[0];
my $xml = $ARGV[1];

$schema = XML::LibXML::Schema->new(location => $xsd);
$parser = XML::LibXML->new;
$doc    = $parser->parse_file($xml);
eval { $schema->validate($doc) };

if ( $@ ) {
warn "xmlfile <$xml> failed validation: $@" if $@; exit(1);
}
else { print "No issues found\n"; }

推荐答案

XML::LibXML 的维护者联系了我,并补充说这是作为错误修复.事实证明,您可以使用命令行实用程序 xmllint 报告多个错误:

I've got contacted by the maintainer of XML::LibXML and filled this is as bug fix. Turns out you can get multiple errors reported by using the command line utility 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

这篇关于Perl XML::LibXML::Schema 将在第一个错误时停止验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆