57

バックグラウンド

スキーマを使用して XML ドキュメントを検証します。

問題

問題の最も単純な形式が 2 つのファイルに示されています。

XML ドキュメント

<?xml version="1.0"?>

<recipe
  xmlns:r="http://www.namespace.org/recipe">

<r:description>
  <r:title>sugar cookies</r:title>
</r:description>

</recipe>

XSD ドキュメント

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
   version="1.0"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:r="http://www.namespace.org/recipe">

  <xsd:complexType name="recipe">
    <xsd:choice>
      <xsd:element name="description" type="descriptionType"
        minOccurs="1" maxOccurs="1" />
    </xsd:choice>
  </xsd:complexType>

  <xsd:complexType name="descriptionType">
    <xsd:all>
      <xsd:element name="title">
        <xsd:simpleType>
          <xsd:restriction base="xsd:string">
            <xsd:minLength value="5" />
            <xsd:maxLength value="55" />
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:element>
    </xsd:all>
  </xsd:complexType>
</xsd:schema>

エラー

xmllintからの完全なエラー メッセージ:

file.xml:4: 要素レシピ: スキーマの有効性エラー: 要素 'recipe': 検証ルートに使用できる一致するグローバル宣言がありません。

質問

特定のスキーマを使用して特定の XML ドキュメントを正常に検証できるようにするための正しい構文 (または欠落しているスキーマ属性) は何ですか?

4

3 に答える 3

30

XML インスタンスを変更する必要があります。あなたの現在のものは、名前空間http://www.namespace.org/recipeにdescriptionと呼ばれるタイプがあると言っています。ただし、XSD 定義では、その名前空間で公開される唯一の型は、レシピdescriptionTypeと呼ばれます。

したがって、XSD スキーマでdescriptionというタイプを定義するか、レシピタイプを正しく参照するようにインスタンスを変更します。

<?xml version="1.0" encoding="utf-8"?>
<r:recipe
  xmlns:r="http://www.namespace.org/recipe">
  <description>
    <title>sugar cookies</title>
  </description>
</r:recipe>

更新これは解決策の半分にすぎません-残りの半分は@Aravindの回答にあります: https://stackoverflow.com/a/8426185/569662

于 2011-12-08T08:19:56.537 に答える
17

ルート要素として使用できるのは、グローバル要素定義のみです。スキーマには複雑な型しかないため、エラーが発生します。<xsd:complexType name="recipe">をに変更

<xsd:element name="recipe">
  <xsd:complexType>
    <xsd:choice>
      <xsd:element name="description" type="descriptionType"
        minOccurs="1" maxOccurs="1" />
    </xsd:choice>
  </xsd:complexType>
</xsd:element>

詳細はこちら

于 2011-12-08T04:31:28.867 に答える
8

私の実践では、次No matching global declaration available for the validation rootの2つのケースで取得しました。

  • XSD に@aravind-r-yarram の回答<xsd:element name="recipe" .../>で説明されているものが含まれていない場合。

  • XML に属性<recipe/>が含まれていない場合。xmlnsそのような場合、を追加するxmlnsと役立ちます:

      <recipe xmlns="http://www.namespace.org/recipe">
          ...
      </recipe>
    
于 2018-02-05T16:19:27.227 に答える