【问题标题】:XML Schema: define type of typesXML Schema:定义类型的类型
【发布时间】:2012-02-07 10:12:04
【问题描述】:

我想定义一个包含指定 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 或至少simpleRestrictionType 中使用localSimpleType 来声明数据类型,但我的XML Schema 编辑器(Visual Studio) 似乎无法识别这些类型。我需要参考另一个 XML Schema 文档吗?我真的很想避免定义整个 XML Schema simpleType 元素及其限制子标签等。

【问题讨论】:

    标签: xml validation types xsd


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您想使用 xsd 还是自己发明并编写自己的解析器?

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

      局部限制最好一次性使用,如果多次使用,为DRY定义一个新类型(不要重复自己)

      例如

      <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"/>
      

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

      哦,我没想到这个,没有验证它,但它应该很接近。

      【讨论】:

        猜你喜欢
        • 2012-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-17
        • 2021-06-06
        • 2019-11-15
        • 1970-01-01
        • 2016-06-21
        相关资源
        最近更新 更多