1

A set of XSD can defines a complex tree-like structure.

For example, to represent this sample model:

LogicFileSample
 +--- Bulk A
         +---- Detail A1
         +---- Detail A2
 +--- Bulk B
         +---- Detail B1
         +---- Detail B2
         ...
 +--- ...   

LogicFileSample and Bulk have some extra attributes, Detail is only a String.

You can implement 2 XSD schema:

1. LogicFileSample.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:bulk="urn:iso:std:iso:20022:tech:xsd:BulkSample"  
    xmlns:logFs="urn:LogicFileSample:xsd:$LogicFileSample.xsd"
    targetNamespace="urn:LogicFileSample:xsd:$LogicFileSample.xsd"
    elementFormDefault="qualified">
    <!-- IMPORT -->
    <xs:import namespace="urn:iso:std:iso:20022:tech:xsd:BulkSample" schemaLocation="BulkSample.xsd"/>

    <xs:element name="LogicFileSampleRoot">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="SuperField01" type="logFs:Max5Text" minOccurs="1" maxOccurs="1"/>
                <xs:element name="SuperField02" type="logFs:Max5Text" minOccurs="1" maxOccurs="1"/>
                <!-- Bulks Container -->
                <xs:element name="BulksContainer" type="bulk:BulkSegmentType" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="Max5Text">
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9A-Z]{5,5}"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

2. BulkSample.xsd

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema
    xmlns="urn:iso:std:iso:20022:tech:xsd:BulkSample"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="urn:iso:std:iso:20022:tech:xsd:BulkSample"
    elementFormDefault="qualified">
    <xs:element name="Document" type="DocumentType"/>
    <xs:complexType name="DocumentType">
        <xs:sequence>
            <xs:element name="BulksContainer" type="BulkSegmentType"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="BulkSegmentType">
        <xs:sequence>
            <xs:element name="BulkHeader"    type="xs:string"/>
            <xs:element name="DetailElement" type="xs:string" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Now, if you create an XML file with n bulks wrapped in a LogicFileSample like this:

<?xml version="1.0" encoding="UTF-8"?>
<logFs:LogicFileSampleRoot 
    xmlns:logFs="urn:LogicFileSample:xsd:$LogicFileSample.xsd" 
    xmlns:bulk="urn:iso:std:iso:20022:tech:xsd:BulkSample"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!-- LogicFileSample info fields -->
  <logFs:SuperField01>ABCDE</logFs:SuperField01>
  <logFs:SuperField02>12345</logFs:SuperField02>
  <!-- 3 BulksContainer -->
  <logFs:BulksContainer>
    <bulk:BulkHeader>BulkHeader_A</bulk:BulkHeader>
    <bulk:DetailElement>DetailElement_A1</bulk:DetailElement>
    <bulk:DetailElement>DetailElement_A2</bulk:DetailElement>
  </logFs:BulksContainer>
    <logFs:BulksContainer>
    <bulk:BulkHeader>BulkHeader_B</bulk:BulkHeader>
    <bulk:DetailElement>DetailElement_B1</bulk:DetailElement>
    <bulk:DetailElement>DetailElement_B2</bulk:DetailElement>
    <bulk:DetailElement>DetailElement_B3</bulk:DetailElement>
  </logFs:BulksContainer>
    <logFs:BulksContainer>
    <bulk:BulkHeader>BulkHeader_C</bulk:BulkHeader>
    <bulk:DetailElement>DetailElement_C1</bulk:DetailElement>
  </logFs:BulksContainer>
</logFs:LogicFileSampleRoot>

...you can validate against "LogicFileSample.xsd" and it works (nice).

Then, you can create a lonely "bulk" XML file, unwrapped. Like this:

<?xml version="1.0" encoding="UTF-8"?>
<blk:Document 
    xmlns:blk="urn:iso:std:iso:20022:tech:xsd:BulkSample"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <blk:BulksContainer>
    <blk:BulkHeader>BulkHeader_X</blk:BulkHeader>
    <blk:DetailElement>DetailElement_X1</blk:DetailElement>
    <blk:DetailElement>DetailElement_X2</blk:DetailElement>
  </blk:BulksContainer>
</blk:Document>

and if you validate against "BulkSample.xsd" the result is OK.

B U T . . .

What happens if you grab the "unwrapped" XML file and you try to validate against "LogicFileSample.xsd"?

Well... it's VALID against "LogicFileSample.xsd"!

Why? First of all, root element is Document (instead of LogicFileSampleRoot) and the mandatory attributes elements SuperField... are not present!

Kindly, somebody can explain the reason why?

Mirko
  • 135
  • 1
  • 8

1 Answers1

1

Why? First of all, root element is "Document" (instead of "LogicFileSampleRoot")

Because any element globally defined in the XSD is allowed to be used as a root element in the XML document. When you import another XSD into the base XSD, the globally defined elements there are then too allowed to be used as root elements.

and the mandatory attributes "SuperField..." are not present!

Mandatory components (elements, in this case, actually, but attributes too) are only mandatory for the associated parent element. So, SuperField is only required for LogicFileSampleRoot, not for the document in general regardless of the parent element.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Your answer is clear: you confirmed my suspicion. Thank you so much! – Mirko Jul 01 '16 at 13:03
  • _"Because any element globally defined in the XSD is allowed to be used as a root element in the XML document"_ Why XSD allow definition of multiple root element, if XML documents must contain one root element at a time? @kjhughes – Mirko Jul 04 '16 at 07:47
  • It's that ***any one*** of the globally defined elements ***might*** be ***the*** root element, not that multiple root elements might exist concurrently. – kjhughes Jul 04 '16 at 13:28
  • Thank you. Probably I asked in a wrong way. I would like to know the reason why XSD schemas give the ability to choice between more than one root. But I think that this is another question... – Mirko Jul 06 '16 at 09:34