1

我在尝试针对以下 xml 文件测试以下 xsd 时遇到问题。是我的工具不好,还是我的 xsd 无法以可预测的方式运行?

软件测试:

  • xmllint(使用 libxml 版本 20707)
  • XML 复制编辑器 1.2.0.6

预期成绩:

  • test.xml 验证
  • 由于域标记中的帐户属性格式错误,test-bad.xml 验证失败

观察到的结果: - test.xml 验证 - test-bad.xml 验证

测试.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>

测试-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的名称为“域”的 s。

这定义了一个,它只能发生在内部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元素上的属性“account”对任何类型都有效。

要仅定义一个元素,最好的方法是将element您的定义放入 acomplexType中,然后引用它(另一种选择是将所有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 回答