Home > database >  What is the intended effect of importing/including an XSD into another XSD file when generating an X
What is the intended effect of importing/including an XSD into another XSD file when generating an X

Time:08-10

When I generate an XML file from an XSD file that has included one or more XSD files, is the resultant XML file supposed to also include the generated XML from the XSD file(s)?

cake.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="https://www.examle.com/bakery"
           targetNamespace="https://www.examle.com/bakery"
           elementFormDefault="qualified">
    <xs:include schemaLocation="bakery.xsd"/>
    <xs:include schemaLocation="cakeType.xsd"/>
    <xs:element name="Cake">
        <xs:complexType>
            <xs:all>
                <xs:element name="cakeName" type="xs:string" minOccurs="1" maxOccurs="1"/>
                <xs:element name="cakeType" type="cakeType" minOccurs="0"/>
                <xs:element name="ingredients" type="xs:string" minOccurs="0"/>
                <xs:element name="instructions" type="xs:string" minOccurs="0"/>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

bakery.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="https://www.examle.com/bakery"
           targetNamespace="https://www.examle.com/bakery"
           elementFormDefault="qualified">
    <xs:element name="Bakery">
        <xs:complexType>
            <xs:all>
                <xs:element name="guid" type="xs:string" minOccurs="1" maxOccurs="1"/>
                <xs:element name="creationDate" type="xs:dateTime" minOccurs="1" maxOccurs="1"/>
                <xs:element name="update" minOccurs="1" maxOccurs="1">
                    <xs:complexType>
                        <xs:all>
                            <xs:element name="updateBy" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="updateDate" type="xs:dateTime" minOccurs="1" maxOccurs="1"/>
                        </xs:all>
                    </xs:complexType>
                </xs:element>
                <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1"/>
                <xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1"/>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

cakeType.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="https://www.examle.com/bakery"
           targetNamespace="https://www.examle.com/bakery"
           elementFormDefault="qualified">
    <xs:element name="CakeType"/>
    <xs:simpleType name="cakeType">
        <xs:restriction base="xs:token">
            <xs:enumeration value="angel"/>
            <xs:enumeration value="pound"/>
            <xs:enumeration value="cheese"/>
            <xs:enumeration value="chiffon"/>
            <xs:enumeration value="genoise"/>
            <xs:enumeration value="sponge"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

I understand the generated XML files would look like this:

cake.xsd.xml:

<bak:Cake xmlns:bak="https://www.examle.com/bakery">
  <bak:cakeName>string</bak:cakeName>
  <!--Optional:-->
  <bak:cakeType>chiffon</bak:cakeType>
  <!--Optional:-->
  <bak:ingredients>string</bak:ingredients>
  <!--Optional:-->
  <bak:instructions>string</bak:instructions>
</bak:Cake>

bakery.xsd.xml:

<bak:Bakery xmlns:bak="https://www.examle.com/bakery">
  <bak:guid>string</bak:guid>
  <bak:creationDate>2008-09-28T19:49:45</bak:creationDate>
  <bak:update>
    <bak:updateBy>string</bak:updateBy>
    <bak:updateDate>2014-09-18T17:18:33</bak:updateDate>
  </bak:update>
  <bak:name>string</bak:name>
  <!--Optional:-->
  <bak:description>string</bak:description>
</bak:Bakery>

cakeType.xsd.xml:

<bak:CakeType xmlns:bak="https://www.examle.com/bakery">anyType</bak:CakeType>

Question: Why wouldn't it generate the XML file this way?

(It is including the generated XML of the included XSD files)

cake.xsd.xml: (Does not validate)

<bak:Cake xmlns:bak="https://www.examle.com/bakery">
  <bak:Bakery xmlns:bak="https://www.examle.com/bakery">
    <bak:guid>string</bak:guid>
    <bak:creationDate>2008-09-28T19:49:45</bak:creationDate>
    <bak:update>
      <bak:updateBy>string</bak:updateBy>
      <bak:updateDate>2014-09-18T17:18:33</bak:updateDate>
    </bak:update>
    <bak:name>string</bak:name>
    <!--Optional:-->
    <bak:description>string</bak:description>
  </bak:Bakery>
  <bak:CakeType xmlns:bak="https://www.examle.com/bakery">anyType</bak:CakeType>
  <bak:cakeName>string</bak:cakeName>
  <!--Optional:-->
  <bak:cakeType>chiffon</bak:cakeType>
  <!--Optional:-->
  <bak:ingredients>string</bak:ingredients>
  <!--Optional:-->
  <bak:instructions>string</bak:instructions>
</bak:Cake>

CodePudding user response:

The include element is used to add multiple schemas with the same target namespace to a document.

cake.xsd needs cakeType.xsd to generate an instance while it may also generate a Bakery type but does not strictly requires it.

XML instance generated from an xsd will have defined elements and no more than that but could have less. For cake.xsd, 3 of them are of xs:string type and 1 of cakeType type defined in cakeType.xsd but valid because of the <xs:include schemaLocation="cakeType.xsd"/> include:

<xs:element name="Cake">
    <xs:complexType>
        <xs:all>
            <xs:element name="cakeName" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="cakeType" type="cakeType" minOccurs="0"/>
            <xs:element name="ingredients" type="xs:string" minOccurs="0"/>
            <xs:element name="instructions" type="xs:string" minOccurs="0"/>
        </xs:all>
    </xs:complexType>
</xs:element>

Generating from cake.xsd returns the same as posted by the OP
XMLBEANS_LIB='/usr/share/java/xmlbeans/' xsd2inst cake.xsd -name Cake

bakery.xsd elements are not present on cake.xsd structure and thus will not appear on it, however, Bakery instance can be generated from cake.xsd due to the include

XMLBEANS_LIB='/usr/share/java/xmlbeans/' xsd2inst cake.xsd -name Bakery

Result

<bak:Bakery xmlns:bak="https://www.examle.com/bakery">
  <bak:guid>string</bak:guid>
  <bak:creationDate>2008-09-28T22:49:45</bak:creationDate>
  <bak:update>
    <bak:updateBy>string</bak:updateBy>
    <bak:updateDate>2014-09-18T20:18:33</bak:updateDate>
  </bak:update>
  <bak:name>string</bak:name>
  <!--Optional:-->
  <bak:description>string</bak:description>
</bak:Bakery>

Minimal Cake instance could be

<bak:Cake xmlns:bak="https://www.examle.com/bakery">
  <bak:cakeName>string</bak:cakeName>
</bak:Cake>
  • Related