1

我有以下xml:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Child name="MyType" compareMode="EQ">Child1</Child>
</Root>

通常为了验证这样一个 xml 会使用以下 xml 模式:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Child">
          <xs:complexType>
            <xs:attribute name="name" type="xs:string" use="required" />
            <xs:attribute name="compareMode" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我想限制Child元素的值并只允许以下内容:Child1Child2Child3

我知道通常可以使用以下模式指定限制:

<xs:restriction base="xs:string">
  <xs:enumeration value="Child1"/>
  <xs:enumeration value="Child2"/>
  <xs:enumeration value="Child3"/>
</xs:restriction>                    

在第一种情况下哪个限制是正确的?

4

2 回答 2

1

这回答了你的问题了吗?

对 xml 模式中的元素同时具有属性和限制

于 2011-02-07T22:07:09.167 回答
1

我错了,因为我没有测试我的答案。我编辑了它:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="ChildContent">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Child1"/>
            <xs:enumeration value="Child2"/>
            <xs:enumeration value="Child3"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="Child">
        <xs:simpleContent>
            <xs:extension base="ChildContent">
                <xs:attribute name="name" type="xs:string" use="required" />
                <xs:attribute name="compareMode" type="xs:string" use="required" />
            </xs:extension>     
        </xs:simpleContent>
    </xs:complexType>
    <xs:element name="Root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Child" type="Child" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

或有限制:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="Child">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="name" type="xs:string" use="required" />
                <xs:attribute name="compareMode" type="xs:string" use="required" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:element name="Root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Child">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:restriction base="Child">
                                <xs:enumeration value="Child1"/>
                                <xs:enumeration value="Child2"/>
                                <xs:enumeration value="Child3"/>
                            </xs:restriction>     
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

在与 google 度过几个小时后:如果没有命名类型,您将无法做到这一点。

于 2011-02-07T22:25:31.377 回答