在针对XSD验证XML时出错

时间:2011-06-26 18:39:15

标签: xml xsd

我有一个简单的XML文件,我想针对XSD进行验证。 当我使用一些随机验证器进行验证时,我总是会收到以下错误:

  

从元素'Entities'开始,找到了无效的内容。预计会有一个“{Entities}”。

这是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>

和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>

从小时开始寻找这个问题,我仍然没有发现错误。你能发现我正确的方向吗? ;)

2 个答案:

答案 0 :(得分:3)

XML Representation of Element Declaration Schema Components

  

{目标命名空间}

     

如果form存在且其实际值qualified,或者form不存在且实际值elementFormDefault祖先上的<schema>qualified,然后是父targetNamespace元素的<schema> [属性]的实际值信息项,或缺席(如果没有),否则缺席

由于elemeFormDefault的默认值为unqualified,除非另有说明,否则本地元素必须是不合格的。

由于您设置了xmlns="http://schemas.testxyzxyz.de/xyz/BusinessModel",因此Entities成为合格人员。 修复是将elementFormDefault="qualified"设置为@polishchuk写道。

答案 1 :(得分:0)

尝试此架构:

<?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>