Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

xml - XSD conditional type assignment default type confusion?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...