【发布时间】: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>
例如,如果我定义<Experiment ID="1" FK="1"/><Experiment ID="2" FK="1"/>,文档验证会引发错误;即多个实验可能不会引用相同的配置文件。但是我为什么还要使用关键关系呢?如果我不能做如此基本的事情,那么关键关系有什么用?
好的,如果不让我这样做,我该怎么办?
edit #1:根据要求,我已经填充了我的代码示例以包含完整的架构和一个基本实例。
编辑#2:很有趣。 SharpDevelop 的 XML 编辑器(相对于 Visual Studio 的)似乎并不反对。它也不反对引用不存在的主键的外键值(它应该是哪个 IMO),但它是一个开始。
【问题讨论】:
-
请提供完整的简短架构和实例,以便我自己做一些实验。谢谢。
-
完成。出于好奇,鉴于我之前的(不完整的)示例,有什么不清楚这应该如何工作?我觉得这足以涵盖所有基础。
标签: xsd foreign-keys primary-key