2

コンテキスト: ソフトウェア ビルド プロセスへの統合のために自動化可能な XSD に対して XSD と XML を検証する方法を見つけようとしています。入力はファイルであり、出力には終了コードまたは確実に正規表現可能なコンソール出力で十分です。Windows環境で運用しています。

XML と XSD の入力として、ウィキペディア (記事「XML スキーマ (W3C)」) から例を取り上げました。

ツールとして、私はXMLLintXercesを試すことにしました。

問題は、XMLLint と Xerces の結果が異なることです。

これは私の質問につながります:ツールを評価するための他のオプションはありますか? どのツールを選択するかをどのように決定すればよいですか?

ウィキペディアからの XML データの例、SimpleAddress.xml:

<?xml version="1.0" encoding="utf-8"?>
<Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="SimpleAddress.xsd">
  <Recipient>Mr. Walter C. Brown</Recipient>
  <House>49</House>
  <Street>Featherstone Street</Street>
  <Town>LONDON</Town>
  <PostCode>EC1Y 8SY</PostCode>
  <Country>UK</Country>
</Address>

ウィキペディアからの XSD スキーマの例、SimpleAddress.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Recipient" type="xs:string" />
        <xs:element name="House" type="xs:string" />
        <xs:element name="Street" type="xs:string" />
        <xs:element name="Town" type="xs:string" />
        <xs:element name="County" type="xs:string" minOccurs="0" />
        <xs:element name="PostCode" type="xs:string" />
        <xs:element name="Country" minOccurs="0">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="IN" />
              <xs:enumeration value="DE" />
              <xs:enumeration value="ES" />
              <xs:enumeration value="UK" />
              <xs:enumeration value="US" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

まず、XMLLint を使用した私のテスト:

$ xmllint.exe --noout --schema SimpleAddress.xsd SimpleAddress.xml
SimpleAddress.xml validates

$ xmllint.exe --noout --schema http://www.w3.org/2001/XMLSchema.xsd SimpleAddress.xsd
SimpleAddress.xsd validates

XMLLint は、検証対象のファイルに意図的に追加の属性またはタグを追加した場合にも、意味のあるエラー結果を返します。

XMLLint を使用して、W3C の XML スキーマ定義をそれ自体に対して検証することも試みました。

$ xmllint.exe --noout --schema http://www.w3.org/2001/XMLSchema.xsd http://www.w3.org/2001/XMLSchema.xsd
http://www.w3.org/2001/XMLSchema.xsd validates

Xerces を使用した 2 番目のテストでは、多くの警告とエラーが発生します。

$ StdInParse.exe -n -s -f -v=always < SimpleAddress.xml
stdin: 1 ms (7 elems, 2 attrs, 19 spaces, 56 chars)

$ StdInParse.exe -n -s -f -v=always < SimpleAddress.xsd
Error at (file stdin, line 2, char 87): no declaration found for element 'xs:schema'
Error at (file stdin, line 2, char 87): attribute 'elementFormDefault' is not declared for element     'xs:schema'
Error at (file stdin, line 2, char 87): attribute '{http://www.w3.org/2000/xmlns/}xs' is not declared  for element 'xs:schema'
Error at (file stdin, line 3, char 30): no declaration found for element 'xs:element'
(…)

次のテストでは、「xmlns:xsi」および「xsi:schemaLocation」属性を SimpleAddress.xsd の開始タグに追加しました。

<xs:schema 
  elementFormDefault="qualified" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd"
>

XMLLint でも同じ結果が得られます。

$ xmllint.exe --noout --schema http://www.w3.org/2001/XMLSchema.xsd SimpleAddress.xsd
SimpleAddress.xsd validates

Xerces はさらに多くの警告とエラーを生成します。現在は W3C のドキュメントにも関係しています。

$ StdInParse.exe -n -s -f -v=always < SimpleAddress.xsd
Warning at (file http://www.w3.org/2001/datatypes.dtd, line 99, char 7): attribute 'id' has already been declared for element 'xs:simpleType'
Warning at (file http://www.w3.org/2001/datatypes.dtd, line 122, char 7): attribute 'id' has already been declared for element 'xs:list'
Warning at (file http://www.w3.org/2001/datatypes.dtd, line 130, char 7): attribute 'id' has already been declared for element 'xs:union'
Warning at (file http://www.w3.org/2001/datatypes.dtd, line 140, char 20): attribute 'id' has already been declared for element 'xs:maxExclusive'
(…)
Error at (file stdin, line 15, char 68): unable to find validator for simple type of attribute 'maxOccurs'
Error at (file stdin, line 16, char 56): unable to find validator for simple type of attribute 'maxOccurs'
Error at (file stdin, line 17, char 50): unable to find validator for simple type of attribute 'maxOccurs'

結論として、XMLLint は私が必要とすることを行っているように見えますが、1 つの大きな欠点があります。XML入力ファイルから対応するxmlns属性を読み取ることができないように見えるため、検証するXSDファイルを別の引数として指定する必要があります。

一方、Xerces があります。これは広く使用されているようで、これらの種類のドキュメントで使用されているものとまったく同じ標準に忠実であると主張していますが、W3C のドキュメントについても多くのエラーや警告が発生します。それで、私はそれを正しく使用しているかどうかを自問しています。XMLLint の必要な追加の引数の回避策を見つける必要がないので、それを機能させたいと思います。

4

1 に答える 1

0

W3C スキーマ ( http://www.w3.org/2001/XMLSchema.xsd )に対して XMLSchema を検証しないでください。代わりに、「空の」XML ドキュメント (ルートのみ) を XMLSchema に対して検証して、XMLSchema にエラーが含まれているかどうかを確認します。

于 2013-03-27T15:02:53.220 に答える