来自XSD的XML错误找不到无效的内容

时间:2014-09-12 10:07:13

标签: xml validation xsd

我正在尝试基于XSD架构定义创建XML文件,在以下链接中找到架构定义

Schema-http://xmlgw.companieshouse.gov.uk/v1-0/schema/forms/CompanyIncorporation-v2-6.xsd

我的XML如下,

   <?xml version="1.0" encoding="UTF-8"?>
  <CompanyIncorporation xmlns="http://xmlgw.companieshouse.gov.uk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk http://xmlgw.companieshouse.gov.uk/v2-1/schema/forms/CompanyIncorporation-v2-6.xsd">
    <CompanyType>BYSHR</CompanyType>
    <CountryOfIncorporation>EW</CountryOfIncorporation>
    <RegisteredOfficeAddress>
    <Premise>38</Premise>
    <Street>Vaughan Road</Street>
    <Thoroughfare>Pentwyn</Thoroughfare>
    <PostTown>Harrow</PostTown>
    <Country>GB-ENG</Country>
    <Postcode>HA1 4EE</Postcode>
    </RegisteredOfficeAddress>
    <Articles>BESPOKE</Articles>
    <RestrictedArticles>false</RestrictedArticles>
    <SameDay>false</SameDay>
    <SameName>false</SameName>
    <NameAuthorisation>false</NameAuthorisation>
    </CompanyIncorporation>

但是当我验证它时会产生错误,

Not valid.
Error - Line 15, 18: org.xml.sax.SAXParseException; lineNumber: 15; columnNumber: 18; 
cvc-complex-type.2.4.a: Invalid content was found starting with element 'SameDay'. One 
of '{"http://xmlgw.companieshouse.gov.uk":Appointment}' is expected.

DEMO

1 个答案:

答案 0 :(得分:3)

您似乎缺少一些要求。

文档类型为CompanyIncorporationType,它只是sequence个元素。使用sequence时,必须指定其中的所有元素,除非minOccurs设置为0。

  

架构元素xsd:sequence定义封闭的元素集应按给定的顺序并根据指定的最小和最大重复计数发生。 (两者的默认值均为1.)

由于错误发生在<SameDay>,请查看xsd中定义的所有(相同级别)元素,<xs:element name="RestrictedArticles"><xs:element name="SameDay">

之间
  • <xs:element name="Appointment" maxOccurs="unbounded"> - 必需

  • <xs:element name="StatementOfCapital" type="StatementOfCapitalType" minOccurs="0"/> - 不需要

  • <xs:element name="Subscribers" type="SubscriberPersonType" minOccurs="0" maxOccurs="unbounded"> - 不需要

  • <xs:element name="Guarantors" type="GuarantorType" minOccurs="0" maxOccurs="unbounded"> - 不需要

  • <xs:element name="Authoriser"> - 必需

因此...

您在<Appointment>

之前缺少两个元素<Authorizer><SameDay>

也就是说,让IDE(或某些xml工具)创建xml shema实现通常会有所帮助。例如,使用Eclipse,它将创建裸骨最小骨架以验证xsd。

这是由Eclipse创建的文件,它具有xml验证的最低要求

<?xml version="1.0" encoding="UTF-8"?>
<CompanyIncorporation ... >
    <CompanyType>PLC</CompanyType>
    <CountryOfIncorporation>EW</CountryOfIncorporation>
    <RegisteredOfficeAddress>RegisteredOfficeAddress</RegisteredOfficeAddress>
    <Appointment>
        <Authentication>Authentication</Authentication>
        <Authentication>Authentication</Authentication>
        <Authentication>Authentication</Authentication>
        <Director />
    </Appointment>
    <Authoriser>
        <Agent>
            <Person>Person</Person>
            <Authentication />
            <Authentication />
            <Authentication />
            <Address>Address</Address>
        </Agent>
    </Authoriser>
    <SameDay>true</SameDay>
</CompanyIncorporation>

<强>更新

解释您在<Appointment><Authoriser>

中看到的元素

<强> <Appointment>

<xs:element name="Appointment" maxOccurs="unbounded">
    <xs:annotation>
        <xs:documentation>Proposed officers</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Authentication" type="PersonalAttributeType" minOccurs="3" maxOccurs="3"/>
            <xs:choice>
                <xs:element name="Director">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:extension base="DirectorAppointmentType"/>
                        </xs:complexContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Secretary" type="SecretaryAppointmentType"/>
                <xs:element name="Member" type="MemberAppointmentType"/>
            </xs:choice>
        </xs:sequence>
    </xs:complexType>
</xs:element>

您可以看到<Appointment>正好有sequence - <Authentication>三次,一个choice<Director>之间,<Secretary><Member>。您必须对xsd进行排序才能真正查看这些元素类型的要求

<强> <Authoriser>

<xs:element name="Authoriser">
    <xs:complexType>
        <xs:choice>
            <xs:element name="Agent" type="AgentType">
            </xs:element>
            <xs:element name="Solicitor" type="AuthoriserType">
            </xs:element>
            <xs:element name="Member" type="AuthoriserType">
            </xs:element>
            <xs:element name="Subscribers">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Subscriber" type="AuthoriserType" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:choice>
    </xs:complexType>
</xs:element>

您可以看到<Authoriser>要求选择<Agent><Member><Solicitor><Subscriber>。您必须对xsd进行排序才能真正查看这些元素类型的要求