0

I found this question which sort of helped but not quite.

XSD - how to allow elements in any order any number of times?

I have the following xml which I want to validate against the XSD. The XSD is valid, but I get error in XML. It saysUnexpected element FirmAgent in element <element>. No further element expected

My requirements are:

  • <element> can appear multiple times
  • Elements inside <element> can some appear multiple times, some are optional, but all should be in any order

I have tried as best as I can to do this. Here is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<archive-elements xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="update1.xsd">
    <element type="1">
        <AdminDistrict>Tes4tveien</AdminDistrict>
        <FirmAgent>Testv12eien</FirmAgent>
        <UpdateByPersNum>Te5stveien</UpdateByPersNum>
        <AttachedFilesListNameInsurance>Test67veien</AttachedFilesListNameInsurance>
        <AttachedFilesListURLInsurance>Testv2ien</AttachedFilesListURLInsurance>
        <Saksnummer>asdf</Saksnummer>
        <StatusSign>asdf</StatusSign>
        <Status>asdf</Status>
        <LocationNr>3</LocationNr>
        <LineNr>01</LineNr>
        <TariffNr>501</TariffNr>
        <CompanyName>Test Person</CompanyName>
        <OrganizationNr>12345678901</OrganizationNr>
        <Email>test@ifasdf.no</Email>
        <UpdateDate>09/30/2011 22:09</UpdateDate>
        <ArchiveType>Godt voksen</ArchiveType>
        <ArchiveApplyFor>Godt voksen Med sykdom</ArchiveApplyFor>
        <TypeOfUpdate>1</TypeOfUpdate>
        <DocID>CF26F4222B84991DC125791B006F333E</DocID>
        <PolicyNr>4664735</PolicyNr>
        <Form>Update1</Form>
        <UpdatedBy>12345678901</UpdatedBy>
        <Status>Effektuert</Status>
        <BehandletKde> </BehandletKde>
        <Status>Effektuert</Status>
        <BehandletKde> sdf</BehandletKde>
        <LabourCosts>123</LabourCosts>
        <SumInsured>asdf</SumInsured>
        <oldSumInsured>50.000</oldSumInsured>
        <OtherChanges>Bla bla</OtherChanges>
        <Coverage>asdf</Coverage>
        <oldCoverage>yrke med lav risiko</oldCoverage>
        <ValidFrom>01.10.11</ValidFrom>
        <County>asdf</County>
        <oldCounty>aasdf</oldCounty>
        <LocatioName>asdf</LocatioName>
        <oldLocatioName>Testveien 1</oldLocatioName>
        <Question1>asdf</Question1>
        <LabourCosts>asdf</LabourCosts>
        <RefNum>asdf</RefNum>
        <RegisterDate>sadf</RegisterDate>
        <OpplastDat>asdf</OpplastDat>
        <SumInsuredInHundred>asdf</SumInsuredInHundred>
        <FraTms>asdf</FraTms>
        <ValidFromType>sadfas2</ValidFromType>
        <Transkode>asdf</Transkode>
        <Period>asdf</Period>
        <BehandlerSign>FCO</BehandlerSign>
    </element>
    <element type="1">
        <AdminDistrict>Tes4tveien</AdminDistrict>
        <FirmAgent>Testv12eien</FirmAgent>
        <UpdateByPersNum>Te5stveien</UpdateByPersNum>
        <AttachedFilesListNameInsurance>Test67veien</AttachedFilesListNameInsurance>
        <AttachedFilesListURLInsurance>Testv2ien</AttachedFilesListURLInsurance>
        <Saksnummer>asdf</Saksnummer>
        <StatusSign>asdf</StatusSign>
        <Status>asdf</Status>
        <LocationNr>3</LocationNr>
        <LineNr>01</LineNr>
        <TariffNr>501</TariffNr>
        <CompanyName>Test Person</CompanyName>
        <OrganizationNr>12345678901</OrganizationNr>
        <Email>test@iasdff.no</Email>
        <UpdateDate>09/30/2011 22:09</UpdateDate>
        <ArchiveType>Godt voksen</ArchiveType>
        <ArchiveApplyFor>Godt voksen Med sykdom</ArchiveApplyFor>
        <TypeOfUpdate>1</TypeOfUpdate>
        <DocID>CF26F4222B84991DC125791B006F333E</DocID>
        <PolicyNr>4664735</PolicyNr>
        <Form>Update1</Form>
        <UpdatedBy>12345678901</UpdatedBy>
        <LabourCosts>123</LabourCosts>
        <SumInsured>asdf</SumInsured>
        <oldSumInsured>50.000</oldSumInsured>
        <OtherChanges>Bla bla</OtherChanges>
        <Coverage>asdf</Coverage>
        <oldCoverage>yrke med lav risiko</oldCoverage>
        <ValidFrom>01.10.11</ValidFrom>
        <County>asdf</County>
        <oldCounty>aasdf</oldCounty>
        <LocatioName>asdf</LocatioName>
        <oldLocatioName>Testveien 1</oldLocatioName>
        <Question1>asdf</Question1>
        <LabourCosts>asdf</LabourCosts>
        <RefNum>asdf</RefNum>
        <RegisterDate>sadf</RegisterDate>
        <OpplastDat>asdf</OpplastDat>
        <SumInsuredInHundred>asdf</SumInsuredInHundred>
        <FraTms>asdf</FraTms>
        <ValidFromType>sadfas2</ValidFromType>
        <Transkode>asdf</Transkode>
        <Period>asdf</Period>
        <BehandlerSign>FCO</BehandlerSign>
    </element>
</archive-elements>

Here is the XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="archive-elements">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="element" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:choice>
                            <xs:element name="AdminDistrict" type="xs:string"/>
                            <xs:element name="FirmAgent" type="xs:string"/>
                            <xs:element name="UpdateByPersNum" type="xs:string"/>
                            <xs:element name="AttachedFilesListNameInsurance" type="xs:string"/>
                            <xs:element name="AttachedFilesListURLInsurance" type="xs:string"/>
                            <xs:element name="Saksnummer" type="xs:string"/>
                            <xs:element name="StatusSign" type="xs:string"/>
                            <xs:element name="LocationNr" type="xs:string" />
                            <xs:element name="LineNr" type="xs:string"/>
                            <xs:element name="TariffNr" type="xs:string"/>
                            <xs:element name="CompanyName" type="xs:string"/>
                            <xs:element name="OrganizationNr" type="xs:string"/>
                            <xs:element name="Email" type="xs:string"/>
                            <xs:element name="UpdateDate" type="xs:string"/>
                            <xs:element name="ArchiveType" type="xs:string"/>
                            <xs:element name="ArchiveApplyFor" type="xs:string"/>
                            <xs:element name="TypeOfUpdate">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="1"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="DocID" type="xs:string"/>
                            <xs:element name="PolicyNr" type="xs:string"/>
                            <xs:element name="Form">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="Update1"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="UpdatedBy" type="xs:string"/>
                            <xs:element name="Status" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                            <xs:element name="BehandletKde" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                            <xs:element name="SumInsured" type="xs:string"/>
                            <xs:element name="oldSumInsured" type="xs:string"/>
                            <xs:element name="OtherChanges" type="xs:string"/>
                            <xs:element name="Coverage" type="xs:string"/>
                            <xs:element name="oldCoverage" type="xs:string"/>
                            <xs:element name="ValidFrom" type="xs:string"/>
                            <xs:element name="County" type="xs:string"/>
                            <xs:element name="oldCounty" type="xs:string"/>
                            <xs:element name="LocatioName" type="xs:string"/>
                            <xs:element name="oldLocatioName" type="xs:string"/>
                            <xs:element name="Question1" type="xs:string"/>
                            <xs:element name="LabourCosts" type="xs:string" minOccurs="0"/>
                            <xs:element name="RefNum" type="xs:string" minOccurs="0"/>
                            <xs:element name="RegisterDate" type="xs:string" minOccurs="0"/>
                            <xs:element name="OpplastDat" type="xs:string" minOccurs="0"/>
                            <xs:element name="SumInsuredInHundred" type="xs:string" minOccurs="0"/>
                            <xs:element name="FraTms" type="xs:string" minOccurs="0"/>
                            <xs:element name="ValidFromType" type="xs:string" minOccurs="0"/>
                            <xs:element name="Transkode" type="xs:string" minOccurs="0"/>
                            <xs:element name="Period" type="xs:string" minOccurs="0"/>
                            <xs:element name="BehandlerSign" type="xs:string" minOccurs="0"/>
                        </xs:choice>
                        <xs:attribute name="type" use="required">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="1"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
Community
  • 1
  • 1
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143

1 Answers1

0

The problem faced in that question and in yours is essentially you are saying, I want an xsd for something with next to no structure...

if you use xs:all, then you can have inner elements in any order, 0 to 1 times. (use minOccurs at 0 or 1 for mandatory / optional). If you want a repeating element then you need a node to bound them e.g. Orders and Order, can't see one in your example though.

xs:choice casues as many problems as it solves, it's fragile and often a git to parse.

I'd be having a serious think about rationalising the xml you are trying to describe, the way you are going is a hiding to nothing.

You could at a push use xs:any to get you away and that start working on firming things up.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39