Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xsd complexType而不是元素上的唯一约束_Xsd - Fatal编程技术网

Xsd complexType而不是元素上的唯一约束

Xsd complexType而不是元素上的唯一约束,xsd,Xsd,我有以下XSD结构: <xs:schema xmlns:ns="http://abc/"> ... <xs:element name="abc"> <xs:complexType> <xs:sequence> <xs:element ref="map"/> </xs:sequence> </xs:complexType> </xs:elem

我有以下XSD结构:

<xs:schema xmlns:ns="http://abc/">
  ...
  <xs:element name="abc">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="map"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  ...
  <xs:element name="map">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="entry" type="ns:MapEntryType" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="entry">
      <xs:selector xpath="entry"/>
      <xs:field xpath="key"/>
    </xs:unique>
  </xs:element>
  <xs:complexType name="MapEntryType">
    <xs:sequence>
      <xs:element name="key" type="xs:string"/>
      <xs:element name="value" type="xs:anyType"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

...
...
这就是它的工作

map
元素现在必须根据包装器调用不同的名称,因此名称有时是
map
,有时是
properties
,有时是
options
,等等

因此,我想将
map
元素泛化

我试着做了以下几点:

  • 制作
    map
    a
    xs:complexType
    并将
    ref
    更改为
    type
    • 这导致
      xs:unique
      未被接受并失败
  • 制作
    map
    a
    xs:complexType
    ,将
    ref
    更改为
    type
    ,并将
    xs:unique
    约束移动到元素定义。
    • 这起到了作用,但导致XSD在文档中存在大量的
      xs:unique

有没有一种方法可以简单地告诉我,我想要一个特定的结构,它包含唯一的元素,而不必到处重复唯一的约束?

简而言之,这是不可能的。XSD1.0和1.1都将标识约束放置在元素下;约束不能全局定义,因此,除了封闭元素的约束外,约束本身没有“重用”。鉴于您的场景(不同的元素名称用于不同的需求),不可能重复使用。

如中所述

XSD1.0和1.1都将标识约束放置在元素下

因此,您必须为每个元素添加xs:unique,但是如果使用XSD 1.1,则只能定义一次完整的xs:unique,然后在其余元素中使用xs:unique ref=“name”。这对您来说是无效的,因为您使用的是XSD 1.0,但我在这里为将来的XSD 1.1用户提供了这个好问题

示例(为清晰起见,已删除名称空间):



好的,谢谢。到目前为止,我已经对每一个可能的名称进行了重新定义,并且像以前一样使用了引用。这似乎是较少的样板选项。实际上在XSD 1.1中,您可以在元素内部定义一个完整的
xs:unique
(带有名称、选择器和字段),然后在其他元素中使用
xs:unique ref=“name”
,而无需再次定义选择器和字段。很高兴知道!不幸的是,我使用的是XSD1.0。当我们更新到XSD 1.1时,我将对文档进行注释以保持此信息可用。由于您不能使用XSD 1.1,另一个解决方案是将
列表包装在名为
的元素中,因此您只定义一次
。不确定这是否是您选择的解决方案。
<xs:element name="map">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="entry" type="MapEntryType" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <!-- Only completely defined once -->
    <xs:unique name="uniqueEntry">
        <xs:selector xpath="entry"/>
        <xs:field xpath="key"/>
    </xs:unique>
</xs:element>

<xs:element name="hashMap">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="entry" type="MapEntryType" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <!-- Referenced here and every other time -->
    <xs:unique ref="uniqueEntry"/>
</xs:element>