XML Schema:定义类型的类型

时间:2012-02-07 10:12:04

标签: xml validation types xsd

我想定义一个包含指定XML模式类型的元素的模式。此问题可能与XML Schema for schemas和此question相关。

这是我到目前为止所拥有的:

<xs:complexType name="metatype">
    <xs:sequence>
      <xs:element name="datatype" type="datatype" minOccurs="0" maxOccurs="1"/>
      <xs:element name="location" type="locationtype" minOccurs="0"maxOccurs="unbounded"/>
      <xs:element name="alias" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required"/>
    <xs:attribute name="editable" type="xs:boolean" default="false" use="optional"/>
    <xs:attribute name="forcedvisible" type="xs:boolean" default="false" use="optional"/>
</xs:complexType>

其中数据类型为:

<xs:complexType name="datatype">
    <xs:sequence>
      <xs:element name="restriction">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="enumeration">
              <xs:complexType>
                <xs:attribute name="value" type="xs:string" use="required"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attribute name="base" type="xs:string" use="required"/>
          <!-- type xs:string is not accurate for XML Schema type -->
        </xs:complexType>
      </xs:element>
    </xs:sequence>
</xs:complexType>

我没有声明数据类型,而是想使用Schema for schemas中的localSimpleType或至少使用simpleRestrictionType,但我的XML Schema编辑器(Visual Studio)似乎没有认识到这些类型。是否需要引用另一个XML Schema文档?我真的想避免为了限制等而定义整个XML Schema simpleType元素及其子标签。

2 个答案:

答案 0 :(得分:1)

您是否想要使用xsd或创建自己的xsd并编写自己的解析器?

您可以定义具有或不具有限制的类型,也可以在已知名称空间(通常包括xs)中为已定义类型添加本地限制。

本地限制最适合一次性使用,如果您多次使用它,请为DRY定义一种新类型(不要自己重复)

e.g。

<xs:simpleType name="mySimpleTypeA">
  <xs:restriction base = "xs:string">
    <xs:enumeration value ="on"/>
    <xs:enumeration value = "off"/>
  </xs:restriction>
</xs:simpleType>

<xs:complexType name="myComplexTypeA>
  <xs:sequence>
    <xs:element name="someElementName">
      <xs:simpleType>
        <xs:restriction base ="xs:string">
          <xs:minLength ="8"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:sequence>
  <xs:attribute name="someAttributeName" use="required" type="mySimpleTypeA"/>
</xs:complexType>

然后你可以像在

中那样使用它们
<xs:element name = "Fred" type ="myComplexTypeA"/>

调用你的元素限制,不会让它成为一个......

哦,我的头顶,没有验证它,但它应该是接近。

答案 1 :(得分:1)

我认为您应该能够编写一个导入S4S的模式以及在其中定义的引用类型。但是,有些工具完全有可能反对。

您不应该做的一件事是尝试处理修改后的S4S,或者在XSD命名空间中添加额外的组件。模式感知工具有权将S4S中的所有内容视为公理化,并向它们提供与其内置的这些组件知识不同的定义可能会造成无法控制的破坏。

相关问题