Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating XML Schema for elements with the same name?

Tags:

xml

xsd

jitterbit

I am trying to create an XSD Schema for an XML document I have received however there are two elements staff which have the same name.

Is there any way to create a schema for this XML even though there are two elements with the same name?

XML:

<contacts>
    <staff count="248" pagesize="284">
    <staff id="1231">
    <Forename>test</Forename>
    <Surname>test</Surname>
    <DateOfBirth>0000-00-00</DateOfBirth>
    <Gender/>
    <Address1/>
    <Address2/>
    <Town/>
    <County/>
    <Telephone/>
    <Mobile/>
    <Email/>
    <Created>0000-06-18 09:46:32</Created>
    <CreatedBy>test</CreatedBy>
    <Updated>2000-06-18 09:46:32</Updated>
    <UpdatedBy>test</UpdatedBy>
    <Archived>0000-00-00 00:00:00</Archived>
    <ArchivedBy/>
    <Postcode/>
    <Age>0</Age>
    <RestrictedRecord>0</RestrictedRecord>
    <Disability_S_24/>
    <Ethnicity_S_25/>
    <Type>8</Type>
    <PersonID>1231</PersonID>
    <TypeName>staff</TypeName>
    </staff>
</staff>
</contacts>

Schema so far:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="contacts">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="staff"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="staff">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" ref="staff"/>
            </xs:sequence>
            <xs:attribute name="count" use="required" type="xs:integer"/>
            <xs:attribute name="pagesize" use="required" type="xs:integer"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="staff">
        <xs:complexType>
            <xs:all  >
                <xs:element ref="Forename" minOccurs="0"/>
                <xs:element ref="Surname" minOccurs="0"/>
                <xs:element ref="DateOfBirth" minOccurs="0"/>
                <xs:element ref="Gender" minOccurs="0"/>
                <xs:element ref="Address1" minOccurs="0"/>
                <xs:element ref="Address2" minOccurs="0"/>
                <xs:element ref="Town" minOccurs="0"/>
                <xs:element ref="County" minOccurs="0"/>
                <xs:element ref="Telephone" minOccurs="0"/>
                <xs:element ref="Mobile" minOccurs="0"/>
                <xs:element ref="Email" minOccurs="0"/>
                <xs:element ref="Created" minOccurs="0"/>
                <xs:element ref="CreatedBy" minOccurs="0"/>
                <xs:element ref="Updated" minOccurs="0"/>
                <xs:element ref="UpdatedBy" minOccurs="0"/>
                <xs:element ref="Archived" minOccurs="0"/>
                <xs:element ref="ArchivedBy" minOccurs="0"/>
                <xs:element ref="Postcode" minOccurs="0"/>
                <xs:element ref="Age" minOccurs="0"/>
                <xs:element ref="RestrictedRecord" minOccurs="0"/>
                <xs:element ref="Disability_S_24" minOccurs="0"/>
                <xs:element ref="Ethnicity_S_25" minOccurs="0"/>
                <xs:element ref="Education_V_2" minOccurs="0"/>
                <xs:element ref="EmploymentTrainingStatus_V_1" minOccurs="0"/>
                <xs:element ref="Type" minOccurs="0"/>
                <xs:element ref="PersonID" minOccurs="0"/>
                <xs:element ref="TypeName" minOccurs="0"/>
            </xs:all>
            <xs:attribute name="id" use="required" type="xs:integer"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="Forename" type="xs:string"/>
    <xs:element name="Surname" type="xs:string"/>
    <xs:element name="DateOfBirth" type="xs:NMTOKEN"/>
    <xs:element name="Gender" type="xs:string"/>
    <xs:element name="Address1" type="xs:string"/>
    <xs:element name="Address2" type="xs:string"/>
    <xs:element name="Town" type="xs:string"/>
    <xs:element name="County" type="xs:string"/>
    <xs:element name="Telephone" type="xs:string"/>
    <xs:element name="Mobile" type="xs:string"/>
    <xs:element name="Email" type="xs:string"/>
    <xs:element name="Created" type="xs:string"/>
    <xs:element name="CreatedBy" type="xs:NCName"/>
    <xs:element name="Updated" type="xs:string"/>
    <xs:element name="UpdatedBy" type="xs:NCName"/>
    <xs:element name="Archived" type="xs:string"/>
    <xs:element name="ArchivedBy" type="xs:string"/>
    <xs:element name="Postcode" type="xs:string"/>
    <xs:element name="Age" type="xs:integer"/>
    <xs:element name="RestrictedRecord" type="xs:integer"/>
    <xs:element name="Disability_S_24">
        <xs:complexType/>
  </xs:element>
    <xs:element name="Ethnicity_S_25">
        <xs:complexType/>
    </xs:element>
    <xs:element name="Type" type="xs:integer"/>
    <xs:element name="PersonID" type="xs:integer"/>
    <xs:element name="TypeName" type="xs:NCName"/>
</xs:schema>
like image 601
Andrew Hoban Avatar asked Oct 20 '25 04:10

Andrew Hoban


1 Answers

If two sibling elements have the same name, then there's a rule in XSD ("element declarations consistent") that says they must also have the same type.

However, if elements appear in different places in the XML, not as siblings, (for example staff/staff versus contacts/staff) then they can have different types; this can be achieved using local element declarations.

like image 112
Michael Kay Avatar answered Oct 22 '25 05:10

Michael Kay