0

simpleType用字符串restriction和命名*pattern*。此 sT basicType应用于多个elements. 问题是其中两个elements需要将类型更改为 base="xs:ID"。

这可以通过创建唯一的 sT 来完成:一个 sT 用于使用基本 ID 的*pattern* restriction字段,另一个 sT 用于需要 ID 基础的两个字段。问题是模式必须在两个 sT 声明中重复。这是我的应用程序中的一个问题,但我想了解是否有另一种方法可用。具体来说,一种允许模式被继承的模式,因此由于所有常见的原因而不会被复制。顺便说一句:任何替代的 XSD 1.0 方法都可以。我不打算将其仅限于simpleType解决方案。

我想做的是有一个单一的模式sT 可以应用于非 ID 字段,并有另一个派生的 sT 为需要 ID 的字段添加 ID 基础,同时继承其他 sT 的模式。因此,模式只在一个地方定义和维护。

FWIW,这是一个 MSXML6 Excel 应用程序。

以下代码片段显示该模式在每个 sT 中都有重复。

问)如何简化这个过程?

<xs:simpleType name="basicType">
    <xs:restriction base="xs:string">
        <xs:pattern value="[A-Z]([A-Z0-9;&#45;]){1,8}">
            <!-- Upper case A~Z followed by 1~8, upper-case alphanumerics including hyphen. No whitespace. -->
        </xs:pattern>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="IDType">
    <xs:restriction base="xs:ID">
        <xs:pattern value="[A-Z]([A-Z0-9;&#45;]){1,8}"/>
    </xs:restriction>
</xs:simpleType>
4

1 回答 1

0

请注意,xs:IDandxs:IDREF是源自 DTD 的遗留类型。

您的问题是 XML Schema身份约束的一个很好的例子。它们与类型层次结构分离,并允许您独立描述节点之间的引用关系。

在模式中,标识约束用xs:key标记键节点 (=ID) 的xs:keyref元素和标记引用键的节点 (=IDREF) 的元素声明。

以这个简单的文档为例:

<root>
  <basic>BASIC</basic>
  <foo id="HELLO"/>
  <bar ref="HELLO"/> <!-- Must reference a <foo> -->
</root>

它可以用以下模式来描述。请注意, basicType它只声明一次,可用于所有节点,无论是简单元素还是关键属性:

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

  <xs:simpleType name="basicType">
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]([A-Z0-9;&#45;]){1,8}"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="basic" type="basicType"/>
        <xs:element name="foo">
          <xs:complexType>
            <xs:attribute name="id" type="basicType"/>
          </xs:complexType>
        </xs:element>
        <xs:element name="bar">
          <xs:complexType>
            <xs:attribute name="ref" type="basicType"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>

    <!-- Identity constraints in the scope of <root> -->

    <xs:key name="basicKey">
      <xs:selector xpath="foo"/> <!-- Apply to <foo> children -->
      <xs:field xpath="@id"/>    <!-- Value of "id" attribute -->
    </xs:key>

    <xs:keyref name="basicKeyref" refer="basicKey">
      <xs:selector xpath="bar"/> <!-- Apply to <bar> children -->
      <xs:field xpath="@ref"/>   <!-- Match "ref" attribute with "id" from basicKey -->
    </xs:keyref>

  </xs:element>
</xs:schema>
于 2017-01-18T23:34:04.230 回答