1

以下の xsd を以下の xml ファイルに対してテストしようとすると問題が発生します。ツールが悪いのでしょうか、それとも xsd が予測どおりに機能していないのでしょうか?

テスト済みのソフトウェア:

  • xmllint (libxml バージョン 20707 を使用)
  • XML コピー エディター 1.2.0.6

予想された結果:

  • test.xml は検証します
  • ドメインタグのアカウント属性の形式が正しくないため、test-bad.xml は検証に失敗します

観察結果: - test.xml の検証 - test-bad.xml の検証

test.xml

<?xml version="1.0" ?>
<!DOCTYPE configuration SYSTEM "configuration.dtd">
<configuration  timestamp="2011-03-23T20:16:57.222" version="2.2" xmlns="http://www.example.com/api/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/api/2.2 configuration.xsd">
    <domain account="4af17ss66f-c841-4b97-a94a-edd7a012176" >
    </domain>
</configuration>

test-bad.xml

<?xml version="1.0" ?>
<!DOCTYPE configuration SYSTEM "configuration.dtd">
<configuration  timestamp="2011-03-23T20:16:57.222" version="2.2" xmlns="http://www.example.com/api/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/api/2.2 configuration.xsd">
    <domain account="totally invalid account" >
    </domain>
</configuration>

構成.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.com/api/2.2" elementFormDefault="qualified" version="1.0" xml:lang="EN" targetNamespace="http://www.example.com/api/2.2">
  <xs:element name="configuration">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="domain"/>
      </xs:sequence>
      <xs:attribute name="timestamp" type="xs:normalizedString" use="optional"/>
      <xs:attribute name="version" type="xs:token" fixed="2.2"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="domain">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0"/>
      </xs:sequence>
      <xs:attribute name="account" type="uid" use="required">
        </xs:attribute>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="uid">
    <xs:restriction base="xs:string">
      <xs:length value="36"/>
      <xs:pattern value="[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

構成.dtd

<!ELEMENT configuration (domain)>
<!ATTLIST configuration
            timestamp           CDATA #IMPLIED
            version             CDATA #FIXED "2.2"
            xmlns               CDATA #IMPLIED
            xmlns:xsi           CDATA #IMPLIED
            xsi:schemaLocation  CDATA #IMPLIED>
<!ELEMENT domain ANY>
<!ATTLIST domain account CDATA #IMPLIED>
4

1 に答える 1

3

element問題は、「ドメイン」という名前で誤って 2 つの異なる を定義してしまったことです。

これは、内部でのみ発生するものを定義しますconfiguration:

    <xs:element name="domain"/>

configurationそして、これはもう一方を定義しますが、これはルート要素としてのみ発生します (要素を削除してルートとして持つと、これを見ることができますdomain- もう検証されません):

<xs:element name="domain">
  <xs:complexType>
    <xs:sequence>
      <xs:any minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="account" type="uid" use="required">
      </xs:attribute>
  </xs:complexType>
</xs:element>

最初の定義はその属性について何も述べていないため、サンプル ドキュメントでは、domain要素の属性「アカウント」はどのタイプでも有効です。

要素を 1 つだけ定義するには、element定義をに作成しcomplexType、それを参照するのが最善の方法です (別の方法は、すべてのcomplexTypeものを最初の定義内に移動することdomainです)。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.com/api/2.2" elementFormDefault="qualified" version="1.0" xml:lang="EN" targetNamespace="http://www.example.com/api/2.2">
  <xs:element name="configuration">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="domain" type="domain"/> <!-- changed here -->
      </xs:sequence>
      <xs:attribute name="timestamp" type="xs:normalizedString" use="optional"/>
      <xs:attribute name="version" type="xs:token" fixed="2.2"/>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="domain"> <!-- and here -->
    <xs:sequence>
      <xs:any minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="account" type="uid" use="required">
      </xs:attribute>
  </xs:complexType>

  <xs:simpleType name="uid">
    <xs:restriction base="xs:string">
      <xs:length value="36"/>
      <xs:pattern value="[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
于 2011-03-25T01:44:40.450 に答える