Why failed round tripping from XSD to XML sample to XSD sample?

Viewed 47

I am new to XML and XSD. When I convert an XSD to XML and then convert the XML that I made to a schema, it does not have the same structure. Should it should be the same structure?

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xs:element name="invoice">
     <xs:complexType>
     <xs:sequence>
     <xs:element maxOccurs="unbounded" minOccurs="1" name="dept" 
    type="deptType"/>
     </xs:sequence>
     </xs:complexType>
     </xs:element>
     <xs:complexType name="deptType">
     <xs:sequence>
     <xs:element name="deptnum" type="xs:string"/>
     <xs:element name="deptname" type="xs:string"/>
     <xs:element name="deptaddress" type="xs:string"/>
     <xs:element name="deptlimit" type="xs:integer"/>
     <xs:element maxOccurs="unbounded" minOccurs="1" name="deptstatement" 
    type="accountType"/>
     </xs:sequence>
     </xs:complexType>
     <xs:complexType name="accountType">
     <xs:sequence>
     <xs:element name="accountid" type="xs:integer"/>
     <xs:element name="accountdate" type="xs:date"/>
     <xs:element maxOccurs="unbounded" minOccurs="0" name="accountcharge" 
    type="chargeType"/>
     </xs:sequence>
     </xs:complexType>
     <xs:complexType name="chargeType">
     <xs:sequence>
     <xs:element name="chargeid" type="xs:string"/>
     <xs:element name="chargeamount" type="xs:decimal"/>
     <xs:element name="chargedate" type="xs:date"/>
     </xs:sequence>
     </xs:complexType>
    </xs:schema>

I converted the above XSD to the XML below

<?xml version="1.0" encoding="utf-8"?>
<invoice>
    <dept>
        <deptnum>1</deptnum>
        <deptname>ac</deptname>
        <deptaddress>hull</deptaddress>
        <deptlimit>1</deptlimit>
        <deptstatement>
            <accountid>1</accountid>
            <accountdate>2020-09-09</accountdate>
            <accountcharge>
                <chargeid>12</chargeid>
                <chargeamount>1.2</chargeamount>
                <chargedate>2020-09-09</chargedate>
            </accountcharge>
        </deptstatement>
    </dept>
</invoice>

but when I convert this XML to schema again in Visual Studio I get this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="invoice">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="dept">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="deptnum" type="xs:unsignedByte" />
                            <xs:element name="deptname" type="xs:string" />
                            <xs:element name="deptaddress" type="xs:string" />
                            <xs:element name="deptlimit" type="xs:unsignedByte" />
                            <xs:element name="deptstatement">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="accountid" type="xs:unsignedByte" />
                                        <xs:element name="accountdate" type="xs:date" />
                                        <xs:element name="accountcharge">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element name="chargeid" type="xs:unsignedByte" />
                                                    <xs:element name="chargeamount" type="xs:decimal" />
                                                    <xs:element name="chargedate" type="xs:date" />
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
1 Answers

For the sort of perfect round tripping you're imagining to be guaranteed to take place, there would have to be

  1. only one valid XML document instance possible for the original XSD, and
  2. only one XSD that could validate the XML document generated in #1.

Neither condition is generally true. Therefore, round tripping functions for generating sample XML from an XSD, and generating a sample XSD for a document instance will generally not result in a return to the original XSD.

Related