将ID添加到派生类型



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

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

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

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

下面的代码片段显示模式在每个sT中都是重复的。

Q) 如何简化这一过程?

<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>
请注意,xs:IDxs:IDREF是源自DTD的遗留类型。

您的问题是XMLSchema标识约束的一个很好的例子。它们与类型层次结构解耦,并允许您独立地描述节点之间的引用关系。

在模式中用xs:key元素声明身份约束,标记关键节点(=ID)的CCD_,其标记引用密钥(=IDREF)的节点。

以这个简单的文档为例:

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

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

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

最新更新