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?