Enzero
Enzero

Reputation: 1141

XSD reusing nodes or duplication

I wanted to know the proper way to do an xsd as certain nodes in my xsd may be reused in other xsd's. Like in Search Item request I will use

<xs:element name="Item">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name" type="xs:string"/>
                <xs:element name="Price" type="xs:double"/>
                <xs:element name="SupplierCode" type="xs:string"/>
                <xs:element name="Supplier" type="xs:string"/>
                <xs:element name="SupplierName" type="xs:string"/>
                <xs:element name="Manufacturer" type="xs:string"/>
                <xs:element ref="CustomerReviews" minOccurs="0"/>
            </xs:sequence>
            <xs:attribute name="model" type="xs:string" use="required"/>
            <xs:attribute name="href" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>

then I might need to use it in another xsd.

So should I repeat it or make a xsd with it in it and reference it?

My Current xsd with include

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://www.itwholesaledeluxe.com" targetNamespace="http://www.itwholesaledeluxe.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:include schemaLocation="Item.xsd"/>
    <xs:element name="Search-Item-Request">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Items">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Item" type="Item"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Then the item xsd partially since its too big to post

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://www.itwholesaledeluxe.com">

        <xs:complexType name="Item">
            <xs:sequence>
                <xs:element name="Name">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="50"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                        </sequence>
                </complexType>
</xs:schema>

Upvotes: 0

Views: 110

Answers (1)

ljgw
ljgw

Reputation: 2771

follow DRY: If it's the same object (in the same namespace): make an xsd and reference it.

That way, if the object is altered at some point, you only need to make your changes once. Also, any code generated for this object only needs to be generated once.

Use the following xsd's as a reference:

Main.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:ns1="http://www.itwholesaledeluxe.com" targetNamespace="http://www.itwholesaledeluxe.com"
    elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:include schemaLocation="Item.xsd" />
    <xs:element name="Search-Item-Request">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Items">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Item" type="ns1:Item" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Item.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified"
    targetNamespace="http://www.itwholesaledeluxe.com">
    <xs:complexType name="Item">
        <xs:sequence>
            <xs:element name="Name">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:maxLength value="50" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Upvotes: 1

Related Questions