首页 > 解决方案 > XML Schema - 两个两个元素的键约束

问题描述

我的 .xml 文件是这样的:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file.xsd">
  <parent id="">
    <child>
      <part>A1</part>
    </child>
  </parent>
  <another-parent name="AAA">
    <part name="A1"/>
    <part name="A2"/>
    <part name="A3"/>
  </another-parent>
  <another-parent name="BBB">
    <part name="A1"/>
  </another-parent>
</root>

我想要的是:

  1. nameof<another-parent>必须是唯一的。没关系。
  2. nameof<part>必须是唯一name<another-parent>。没关系。
  3. <part name="A1">inside<child>必须是<another-parent>. 而我不能那样做。

对于第一点,我使用了以下内容并且它可以正常工作。

<xsd:key name="anotherParentKey">
    <xsd:selector xpath="another-parent"/>
    <xsd:field xpath="@name"/>
</xsd:key>

对于第二个,我在元素声明中使用了这个:

<xsd:key name="partKey">
    <xsd:selector xpath="part"/>
    <xsd:field xpath="./@name"/>
</xsd:key>

现在可以了。

但是对于第三部分,我尝试将此代码放入(第一个共同祖先)但它不起作用:

<xsd:keyref name="roadSegmentRef" refer="roadSegmentKey">
    <xsd:selector xpath="identifiedEntity/place/roadSegment" />
    <xsd:field xpath="." />
</xsd:keyref>

但我得到的只是:

cvc-identity-constraint.4.3: Key 'partRef' with value 'A1' not found for identity constraint of element 'parent'.   file.xml    /sheet/xsd  line 19 XML Problem

标签: xmlxsdschemaxmlschemaset

解决方案


你没有告诉我们你把这些声明放在哪里,我猜你把它们放在错误的地方。

如果您希望某个 Y 中的每个 X 都具有 Z 的唯一值,那么您的键/唯一声明需要进入 Y 的元素声明;选择器需要从Y开始选择X,字段需要从X开始选择Z。


推荐阅读