Error while validating XML against XSD

守給你的承諾、 提交于 2020-01-03 13:31:09

问题


I got a simple XML file that I want to validate against an XSD. When I validate with some random validator, I always get the following error:

Invalid content was found starting with element 'Entities'. One of '{Entities}' is expected.

Here's the XML:

<BusinessModel xmlns="http://schemas.testxyzxyz.de/xyz/BusinessModel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entities>
    <Entity Name="Customer">
        <Attribute Name="Forename" Type="String" />
        <Attribute Name="Surname" Type="String" />
        <Attribute Name="Birthday" Type="Date" />
    </Entity>
</Entities>
</BusinessModel>

As well as the XSD:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://schemas.testxyzxyz.de/xyz/BusinessModel"
        xmlns="http://schemas.testxyzxyz.de/xyz/BusinessModel">

<xsd:element name="BusinessModel" type="BusinessModelType" />

<xsd:complexType name="BusinessModelType">
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
        <xsd:element name="Entities" type="EntitiesType" />
    </xsd:choice>
</xsd:complexType>

<xsd:complexType name="EntitiesType">
    <xsd:sequence>
        <xsd:element name="Entity" type="EntityType" maxOccurs="unbounded" />
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="AttributeType">
    <xsd:attribute name="Name" type="xsd:string" use="required" />
    <xsd:attribute name="Type" type="xsd:string" use="required" />
</xsd:complexType>

<xsd:complexType name="EntityType">
    <xsd:sequence>
        <xsd:element name="Attribute" type="AttributeType" maxOccurs="unbounded" minOccurs="1" />
    </xsd:sequence>
        <xsd:attribute name="Name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:schema>

Looking for that issue since hours, and I still don't find an error. Can you spot me the right direction? ;)


回答1:


XML Representation of Element Declaration Schema Components:

{target namespace}

If form is present and its actual value is qualified, or if form is absent and the actual value of elementFormDefault on the <schema> ancestor is qualified, then the actual value of the targetNamespace [attribute] of the parent <schema> element information item, or absent if there is none, otherwise absent.

Since the default value of elemeFormDefault is unqualified, unless otherwise specified the local elements must be unqualified.

Since you set the xmlns="http://schemas.testxyzxyz.de/xyz/BusinessModel", the Entities became qualified. The fix is to set elementFormDefault="qualified" as @polishchuk wrote.




回答2:


Try this schema:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://schemas.testxyzxyz.de/xyz/BusinessModel" xmlns:b="http://schemas.testxyzxyz.de/xyz/BusinessModel">
  <xsd:element name="BusinessModel" type="b:BusinessModelType" />
  <xsd:complexType name="BusinessModelType">
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
      <xsd:element name="Entities" type="b:EntitiesType" />
    </xsd:choice>
  </xsd:complexType>
  <xsd:complexType name="EntitiesType">
    <xsd:sequence>
      <xsd:element name="Entity" type="b:EntityType" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="AttributeType">
    <xsd:attribute name="Name" type="xsd:string" use="required" />
    <xsd:attribute name="Type" type="xsd:string" use="required" />
  </xsd:complexType>
  <xsd:complexType name="EntityType">
    <xsd:sequence>
      <xsd:element name="Attribute" type="b:AttributeType" maxOccurs="unbounded" minOccurs="1" />
    </xsd:sequence>
    <xsd:attribute name="Name" type="xsd:string" use="required" />
  </xsd:complexType>
</xsd:schema>


来源:https://stackoverflow.com/questions/6485955/error-while-validating-xml-against-xsd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!