XSD conditional type assignment default type confusion?

余生颓废 提交于 2019-12-24 09:35:13

问题


I try to design a XSD with CTA. I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="test.xsd">
    <person version="1">
        <firstname>toto</firstname>
        <lastname>tutu</lastname>
    </person>
    <person version="2">
        <firstname>toto</firstname>
        <lastname>tutu</lastname>
        <birthdate>2017-12-18</birthdate>
    </person>
</persons>

The XSD looks like:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
    elementFormDefault="qualified"
    vc:minVersion="1.1"> 

    <xs:complexType name="person_v1">
        <xs:sequence>
            <xs:element name="firstname" type="xs:token"/>
            <xs:element name="lastname" type="xs:token"/>
        </xs:sequence>
        <xs:attribute name="version" type="xs:token" use="required" fixed="1"/>
    </xs:complexType>

    <xs:complexType name="person_v2">
        <xs:sequence>
            <xs:element name="firstname" type="xs:token"/>
            <xs:element name="lastname" type="xs:token"/>
            <xs:element name="birthdate" type="xs:date"/>
        </xs:sequence>
        <xs:attribute name="version" type="xs:token" use="required" fixed="2"/>
    </xs:complexType>

    <xs:element name="person">
        <xs:alternative test="@version=1" type="person_v1"/>        
        <xs:alternative test="@version=2" type="person_v2"/>
    </xs:element>

    <xs:element name="persons">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="person" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

The XML is validated against the XSD, but in the XSD:

  1. If I delete the version attribute, it validates,
  2. If I replace the version 1 by 3, it validates,
  3. If I delete all the content of person, it validates.

There must be something missing on my XSD.


回答1:


  1. If I delete the version attribute, it validates,

As it should, because when no xs:alternative tests match, person can be xs:anyType per Common Mapping Rules for Element Declarations.

  1. If I replace the version 1 by 3, it validates,

As it should for the same reason as #1.

  1. If I delete all the content of person, it validates.

No, it doesn't. You may not have truly associated the XML document with your XSD, or you may have accidently combined experiment #3 with experiments #1 or #2.

Updated XSD

The following updated XSD specifies a default type of person rather than xs:anyType to avoid the surprises #1 and #2. Note that it also uses extension to consolidate common parts of the declarations of person_v1 and person_v2 into person_v and to satisfy the requirement that the alternative types must be derived from a common base class. (When unspecified, it's xs:anyType and typically goes unnoticed.)

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           elementFormDefault="qualified"
           vc:minVersion="1.1"> 
  <xs:complexType name="person_v">
    <xs:sequence>
      <xs:element name="firstname" type="xs:token"/>
      <xs:element name="lastname" type="xs:token"/>
    </xs:sequence>
    <xs:attribute name="version" type="xs:token" use="required"/>
  </xs:complexType>
  <xs:complexType name="person_v1">
    <xs:complexContent>
      <xs:extension base="person_v"/>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="person_v2">
    <xs:complexContent>
      <xs:extension base="person_v">
        <xs:sequence>
          <xs:element name="birthdate" type="xs:date"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:element name="person" type="person_v">
    <xs:alternative test="@version=1" type="person_v1"/>        
    <xs:alternative test="@version=2" type="person_v2"/>
  </xs:element>
  <xs:element name="persons">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="person" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Update: Here's an update to person_v to require that version be 1 or 2 to address a concern in the comments:

  <xs:complexType name="person_v">
    <xs:sequence>
      <xs:element name="firstname" type="xs:token"/>
      <xs:element name="lastname" type="xs:token"/>
    </xs:sequence>
    <xs:attribute name="version" use="required">
      <xs:simpleType>
        <xs:restriction base="xs:integer">
          <xs:minInclusive value="1"/>
          <xs:maxInclusive value="2"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
  </xs:complexType>


来源:https://stackoverflow.com/questions/47866026/xsd-conditional-type-assignment-default-type-confusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!