如何在 Perl 中使用 XSD 验证 XML? [英] How can I validate XML with XSD in Perl?

查看:40
本文介绍了如何在 Perl 中使用 XSD 验证 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于大多数 Perl 程序员来说,这可能是一个简单的问题,到目前为止,我只使用了两周的 Perl,对 Perl 包非常不熟悉.

This may be a simple question for most Perl programmers, I have only used Perl for two weeks so far and am very unfamiliar with the Perl packages.

我有一个简单的 XSD 文件,如下所示:

I have a simple XSD file as below:

<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    <xsd:element name="elementname">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="field1" type="xsd:integer" minOccurs="0" maxOccurs="1"/>
                <xsd:element name="field2" type="xsd:string" minOccurs="0" maxOccurs="1"/>              
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

我想用上述 XSD 验证 XML 文件,以确保这是一个有效的 XML.我应该使用什么 Perl 模块?我更喜欢在 *nix 上的 ActivePerl 和 Perl 上都可用的模块.发布一些代码片段会非常有帮助.

I would love to validate an XML file with the above XSD to ensure this is a valid XML. What Perl module should I use? I prefer a module that is available both on ActivePerl and Perl on *nix. Would be very helpful to post some code snippets.

谢谢

推荐答案

我认为你需要 XML::Validator::Schema 来自 CPAN.这是自述文件,并安装:>

I think you need XML::Validator::Schema from CPAN. Here's the README, and to install:

perl -MCPAN -e 'install XML::Validator::Schema'

这篇关于如何在 Perl 中使用 XSD 验证 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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