如何在XML Schema中扩展xs:list

时间:2013-11-10 12:49:07

标签: xml xsd

我正在学习如何编写XML模式,我想定义一个XML模式来验证这样的XML结构:

<mylist myattr="1">1 2 3 4 5 6 7</mylist>

因此,我试图定义一个使用complexType的{​​{1}},并且有一个属性。

这是我提出的架构:

list

当我使用http://www.freeformatter.com/xml-validator-xsd.html针对架构验证XML时,我获得了错误

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

  <xs:complexType name="mylist-type">
    <xs:simpleContent>
      <xs:extension base="xs:list" >
        <xs:attribute name="myattr" type="xs:integer"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:element name="mylist" type="mylist-type"/>
</xs:schema>

但是,如果我只是将Src-resolve.4.2: Error Resolving Component 'xs:list'. It Was Detected That 'xs:list' Is In Namespace 'http://www.w3.org/2001/XMLSchema', But Components From This Namespace Are Not Referenceable From Schema Document 'null'. If This Is The Incorrect Namespace, Perhaps The Prefix Of 'xs:list' Needs To Be Changed. If This Is The Correct Namespace, Then An Appropriate 'import' Tag Should Be Added To null'. 更改为xs:list,那么架构会毫无问题地进行验证,这对我提出的问题是否真的是命名空间问题。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

使用

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

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

  <xs:complexType name="mylist-type">
    <xs:simpleContent>
      <xs:extension base="int-list" >
        <xs:attribute name="myattr" type="xs:integer"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:element name="mylist" type="mylist-type"/>
</xs:schema>

所以基本上首先定义一个特定的列表类型,然后在你的扩展中使用该列表类型。

答案 1 :(得分:0)

草稿架构将mylist-type定义为xs:list类型的扩展。但是XSD名称空间中没有名为list的数据类型。当您替换名称xs:string时错误消失的原因是一个名为xs:string的类型。

当你的处理器告诉你“也许'xs:''的前缀'需要改变'时,它猜测你可能在其他命名空间中使用list类型。

[附录] xs:list,你问,如果它不是一个类型?它是从其他简单类型派生简单类型的三种方法之一。例如,以下类型声明描述了由空格分隔的整数列表。 xs:list元素上的itemType属性说明列表项的类型。

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

与XSD中一样,也可以使用匿名类型代替命名类型;在这种情况下,相关的simpleType元素显示为xs:list的子元素。在下面的声明中,内部simpleType元素声明一个类型,其值为0到100之间的整数(包括课程中测验所用的成绩等级),外部simpleType元素声明一个由列表组成的类型这样的数字。

<xs:simpleType name="list-of-grades">
  <xs:list>
    <xs:simpleType>
      <xs:restriction base="xs:integer">
        <xs:minInclusive value="0"/>
        <xs:maxInclusive value="100"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:list>
</xs:simpleType>
相关问题