XML 架构:基于 xs:anyType 的限制有什么好处

XML Schema: what is the benefit of a restriction based on xs:anyType

在一个项目中,我们必须连接到一个 web 服务,该服务在其 wsdl 中定义了许多基于 xs:anyType 限制的类型,如下所示:

<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="myElement" type="myTypeDef" />

    <xs:complexType name="myTypeDef">
          <xs:complexContent>
              <xs:restriction base="xs:anyType">
                  <xs:sequence>
                      <xs:element name="elementOne" type="xs:string"/>
                      <xs:element name="elementTwo" type="xs:string"/>
                  </xs:sequence>
              </xs:restriction>
          </xs:complexContent>
    </xs:complexType>

</xs:schema>

与像

这样的普通类型定义有什么区别
    <xs:complexType name="myTypeDefPlain">
        <xs:sequence>
            <xs:element name="elementOne" type="xs:string"/>
            <xs:element name="elementTwo" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

据我了解,这两种类型定义是相同的。真的吗?是否有使用基于 xs:anyType 的限制的用例?

在这种情况下,none。检查 the spec, §3.4.2,特别是示例:

<xs:complexType name="length2">
 <xs:complexContent>
  <xs:restriction base="xs:anyType">
   <xs:sequence>
    <xs:element name="size" type="xs:nonNegativeInteger"/>
    <xs:element name="unit" type="xs:NMTOKEN"/>
   </xs:sequence>
  </xs:restriction>
 </xs:complexContent>
</xs:complexType>

<xs:complexType name="length3">
 <xs:sequence>
  <xs:element name="size" type="xs:nonNegativeInteger"/>
  <xs:element name="unit" type="xs:NMTOKEN"/>
 </xs:sequence>
</xs:complexType>

length3 is the abbreviated alternative to length2: they correspond to identical type definition components.