【问题标题】:Why won't XML Schema allow me to define a one-to-many key? And how do I do so?为什么 XML Schema 不允许我定义一对多键?我该怎么做?
【发布时间】:2010-12-20 20:16:12
【问题描述】:

我被 XML Schema 中令人沮丧的任意限制所困扰。出于某种原因,它坚持 PK-FK 关系必须是一对一的。为什么?

例如,给定架构:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="FutureSchema"
    targetNamespace="http://tempuri.org/FutureSchema.xsd"
    elementFormDefault="unqualified"
    xmlns="http://tempuri.org/FutureSchena.xsd"
    xmlns:mstns="http://tempuri.org/FutureSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="FUTUREFILE">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Configuration">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Experiments">

                <xs:complexType>
                  <xs:sequence>

                    <xs:element name="Experiment">

                      <xs:complexType>
                        <xs:attribute name="ID" type="xs:integer"/>
                        <xs:attribute name="Profile" type="xs:integer"/>
                      </xs:complexType>

                      <xs:keyref name="dummy" refer="LP">
                        <xs:selector xpath="Experiment"/>
                        <xs:field xpath="@Profile"/>
                      </xs:keyref>

                    </xs:element>

                  </xs:sequence>
                </xs:complexType>

                <xs:key name="LP">
                  <xs:selector xpath="*/Configuration/Profiles/Profile"/>
                  <xs:field xpath="@ID"/>
                </xs:key>

              </xs:element>
              <xs:element name="Profiles">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Profile">
                      <xs:complexType>
                        <xs:attribute name="ID" type="xs:integer"/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

还有一个例子:

<?xml version="1.0" encoding="utf-8" ?>
<b:FUTUREFILE xmlns:b="http://tempuri.org/FutureSchena.xsd">
  <Configuration>
    <Experiments>
      <Experiment ID="1" Profile="1"/>
      <Experiment ID="2" Profile="1"/>
    </Experiments>
    <Profiles>
      <Profile ID="1"/>
    </Profiles>
  </Configuration>
</b:FUTUREFILE>

例如,如果我定义&lt;Experiment ID="1" FK="1"/&gt;&lt;Experiment ID="2" FK="1"/&gt;,文档验证会引发错误;即多个实验可能不会引用相同的配置文件。但是我为什么还要使用关键关系呢?如果我不能做如此基本的事情,那么关键关系有什么用?

好的,如果不让我这样做,我该怎么办?

edit #1:根据要求,我已经填充了我的代码示例以包含完整的架构和一个基本实例。

编辑#2:很有趣。 SharpDevelop 的 XML 编辑器(相对于 Visual Studio 的)似乎并不反对。它也不反对引用不存在的主键的外键值(它应该是哪个 IMO),但它是一个开始。

【问题讨论】:

  • 请提供完整的简短架构和实例,以便我自己做一些实验。谢谢。
  • 完成。出于好奇,鉴于我之前的(不完整的)示例,有什么不清楚这应该如何工作?我觉得这足以涵盖所有基础。

标签: xsd foreign-keys primary-key


【解决方案1】:

什么不清楚?...我的英语,就是这样:-)

一些remarqs,我不确定它的解决方案:

例如,最好使用xmlns:b="http://tempuri.org/FutureSchema.xsd"&gt;,而不是xmlns:b="http://tempuri.org/FutureSchena.xsd"&gt; (n -> m)。

此外,最好将 b: 放在所有元素名称的前面。

根据你的架构,Experiments中不能有两个Experiment,只有一个。

在 xsd 上,输入&lt;xs:keyref name="dummy" refer="mstns:LP"&gt;,这对我最有效;这是因为 xpath 表达式不理解默认命名空间,请参阅Correct way to use key in xsd

【讨论】:

  • 我很抱歉;我的模式是我在工作中编写的模式的复制版本,因此我没有完全正确。该版本确实允许使用 不同 配置文件进行多次实验。 schema 中的拼写错误也是如此。
  • 好了。这让我得到了正确的答案——由于默认命名空间问题,它似乎没有正确应用密钥约束。此外,再次重写了相同的逻辑,.net xml DOM 现在可以正确接受一对多的 PK-FK。第一次出了什么问题,我真的不知道。尽管如此,它现在仍然有效。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 2021-03-24
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
相关资源
最近更新 更多