xmllint:针对两个XSD模式(信封/有效负载)验证XML文件 [英] xmllint : validate an XML file against two XSD schemas (envelope / payload)

查看:113
本文介绍了xmllint:针对两个XSD模式(信封/有效负载)验证XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xmllint 进行一些验证,并且我有一个XML实例文档,需要针对该实例进行验证两种模式:一种用于外部信封"(包括 any 元素),另一种用于特定的有效载荷.假设 A.xsd 是信封模式, B.xsd 是有效负载模式(可能有不同的有效负载),而 ab.xml 是有效的XML实例文档(我在文章末尾提供了一个示例).

I am using xmllint to do some validations and I have an XML instance document which needs to validate against two schemas: one for the outer "envelope" (which includes an any element) and one for the particular payload. Say A.xsd is the envelope schema, B.xsd a payload schema (there are different possible payloads) and ab.xml a valid XML instance document (I provide an example at the end of the post).

我在同一个目录中本地拥有所有三个文件,并且正在使用 xmllint 进行验证,提供 schema 参数作为外部(信封)模式的位置:

I have all three files locally available in the same directory and am using xmllint to perform the validation, providing as the schema argument the location of the outer (envelope) schema:

xmllint -schema A.xsd ab.xml

...但是,尽管我在实例文档中同时提供了A.xsd和B.xsd的位置(使用 xsi:schemaLocation 元素) xmllint 失败找到它并抱怨:

... yet, although I provide the location of both A.xsd and B.xsd in the instance document (using the xsi:schemaLocation element) xmllint fails to find it and complains:

ab.xml:8: element person: Schemas validity error : Element '{http://www.example.org/B}person': No matching global element declaration available, but demanded by the strict wildcard.
ab.xml fails to validate

因此,显然 xmllint 没有读取 xsi:schemaLocation 元素.我了解可以使用目录配置 xmllint ,但是我无法获得 xmllint 来找到这两种模式.

So apparently xmllint is not reading the xsi:schemaLocation element. I understand that xmllint can be configured with catalogs but I failed to get xmllint to find both schemas. How should I get xmllint to take into account both schemas when validating the instance document or is there another command line utility or graphical tool I could use instead?

<?xml version="1.0" encoding="UTF-8"?>
<schema elementFormDefault="qualified" 
        xmlns               ="http://www.w3.org/2001/XMLSchema"
        xmlns:a             ="http://www.example.org/A"
        targetNamespace ="http://www.example.org/A">

       <element name="someType" type="a:SomeType"></element>

        <complexType name="SomeType">
            <sequence>
                <any namespace="##other" processContents="strict"/>
            </sequence>
        </complexType>
</schema>

B.xsd-有效负载架构

<?xml version="1.0" encoding="UTF-8"?>
<schema elementFormDefault="qualified"
    xmlns          ="http://www.w3.org/2001/XMLSchema"
    xmlns:b        ="http://www.example.org/B"
    targetNamespace="http://www.example.org/B"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <element name="person" type="b:PersonType"></element>
    <complexType name="PersonType">
        <sequence>
                <element name="firstName" type="string"/>
                <element name="lastName"  type="string"/>
        </sequence>
    </complexType>
  </schema>

ab.xml-实例文档

<?xml version="1.0" encoding="UTF-8"?>
<a:someType xmlns:a="http://www.example.org/A"
        xmlns:b="http://www.example.org/B"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.example.org/A A.xsd
                            http://www.example.org/B B.xsd">

            <b:person>
                <b:firstName>Mary</b:firstName>
                <b:lastName>Bones</b:lastName>
            </b:person>

</a:someType>

推荐答案

我退出了 xmllint ,改用了 Xerces .

我下载了 Xerces tarball,并将其分解到一些本地文件夹后,我创建了以下 validate 脚本基于此建议(来自网络存档-原始链接现已消失):

I downloaded Xerces tarball and after exploding it to some local folder I created the following validate script based on this suggestion (from web archive - original link being now dead):

#!/bin/bash
XERCES_HOME=~/software-downloads/xerces-2_11_0/
echo $XERCES_HOME
java -classpath $XERCES_HOME/xercesImpl.jar:$XERCES_HOME/xml-apis.jar:$XERCES_HOME/xercesSamples.jar sax.Counter $*

然后使用以下命令针对两种模式对 ab.xml 文件进行验证:

The ab.xml file is then validated, against both schemas, with the following command:

 validate -v -n -np -s -f ab.xml

Xerces正在从 ab.xml 中的 xsi:schemaLocation 元素读取架构位置,因此不需要在命令行调用中提供它们.

Xerces is reading the schema locations from the xsi:schemaLocation element in ab.xml so they don't need to be provided in the command line invocation.

这篇关于xmllint:针对两个XSD模式(信封/有效负载)验证XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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