【问题标题】:xml schema against XML error针对 XML 错误的 xml 架构
【发布时间】:2012-12-08 08:49:03
【问题描述】:

我正在针对一个 xml 文件编写一个 xml 架构。我收到以下代码的此错误,但无法弄清楚原因。有什么建议吗?

cvc-type.3.1.1:元素'employees'是一个简单类型,所以它不能有属性,除了那些命名空间名称与'http://www.w3.org/2001/XMLSchema-instance ' 并且其 [本地名称] 是 'type'、'nil'、'schemaLocation' 或 'noNamespaceSchemaLocation' 之一。但是,找到了属性“essns”。发现问题始于:simpleType。

<xs:element name="employees" >
      <xs:simpleType>
        <xs:list itemType ="xs:integer"/>
      </xs:simpleType>
      </xs:element>
      <xs:element name= "projectsControlled">
      <xs:simpleType>
        <xs:list itemType ="xs:integer" />
      </xs:simpleType>
      </xs:element>

以下是xml代码

<employees essns="888665555"/>
<projectsControlled pnos="20"/>

【问题讨论】:

  • 错误信息看起来很明确。类型 employees 不能有 essns 属性,因为架构不允许。

标签: xml xsd


【解决方案1】:

元素可能有简单类型或复杂类型。

具有简单类型的元素只不过是适当类型的有效值的包装器。不允许有其他内容,不允许有子元素,不允许有属性。

也就是说,只有复杂类型管理的元素才允许有属性。 (如您的错误消息中所述,xsi:nil、xsi:type、xsi:schemaLocation 和 xsi:noNamespaceSchemaLocation 存在异常。)

您的 'employees' 元素被声明为具有简单类型:整数列表。因此允许它包含一个整数列表,但您没有为它声明任何属性。如果您想这样做,您可以将其声明为具有“具有简单内容的复杂类型”——本质上是一种通过添加属性来扩展简单类型的复杂类型。

<xs:simpleType name="list-of-integers">
  <xs:list itemType="xs:integer"/>
</xs:simpleType>

<xs:element name="employees">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="tns:list-of-integers">
        <xs:attribute name="essns" 
                      type="tns:list-of-integers"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

或者您可能打算将employees 声明为具有名为essns 的属性的空元素,其值可以是整数列表。在这种情况下,您不想要具有简单内容的复杂类型,因为您想要的不是简单内容,而是no 内容。

<xs:element name="employees-sib">
  <xs:complexType mixed="false">
    <xs:sequence/>
    <xs:attribute name="essns" 
                  type="tns:list-of-integers"/>
  </xs:complexType>
</xs:element>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    相关资源
    最近更新 更多