XSD 限制规则 [英] XSD Restricted Rule
问题描述
有没有办法在 xsd 中创建像外键受限"这样的规则?我需要检查是否存在与节点的属性使用"相等的属性值.
There is a way to create a rule like 'foreign key restricted' in xsd? I need to check the existence of an attribute value that is equal to the attribute 'use' of a node.
<node use="..." />
<otherNode name="..." />
看上面的例子,属性设置为'node'属性'use',这个属性的值必须等于'name'属性中的'otherNode'
See the example above, the attribute is set to 'node' attribute 'use', the value of this attribute must be equal to 'otherNode' in the 'name' attribute
推荐答案
设置use属性的类型为xs:IDREF
Set the type of the use attribute to xs:IDREF
以及 xs:ID 的 name 属性.
and the name attribute to xs:ID.
还有一个 xs:IDREFS 类型.
There's also an xs:IDREFS type.
一个例子
<xsd:element name="quote">
<xsd:complexType>
<!--content model-->
<xsd:attribute name="ref" type="xsd:IDREF"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="footnote">
<xsd:complexType>
<!--content model-->
<xsd:attribute name="id" type="xsd:ID" use="required"/>
</xsd:complexType>
...
给予
<quote ref="fn1">...</quote>
<footnote id="fn1">...</footnote>
所以引用必须有一个 ref 参数,在这种情况下它指向一个脚注
So quote must have a ref parameter, which in this case points to a footnote
嗯,上面的内容似乎是针对 DTD 的,但我发现它不起作用
Hmm would appear that the above was for DTD and doesn't work however I found this
真键表示
如果您曾尝试使用 DTD 描述具有复杂关系映射的关系数据库,您可能不得不使用 ID-IDREF 指向机制.例如,在两个实体通过关联表以多对多方式关联的结构中(例如,贷款申请中的借款人和资产),简单的 XML 父子关系是不够的.但是,ID 和 IDREF 有其自身的弱点:ID 在整个文档中必须是唯一的,并且 IDREF 声明没有指定 IDREF 属性的实例必须引用的元素类型.XML Schema 提供了一种指定这些指向关系的方法,其方式与在关系数据库中声明外键关系的方式大致相同.例如,假设您有一个外键关系,您无法在我们的 XML 中使用简单的父子关系来表达它.您可以声明两个相关元素,如清单 3 所示:
If you have ever attempted to describe a relational database with a complex relationship map using a DTD, you've likely had to use the ID-IDREF pointing mechanism. For example, in a structure where two entities are related in a many-to-many way through a relating table (borrowers and assets on a loan application, for example), the simple XML parent-child relationship is insufficient. However, IDs and IDREFs have their own weaknesses: IDs must be unique across an entire document, and IDREF declarations do not specify the type of element an instance of the IDREF attribute must reference. XML Schema provides a way to specify these pointing relationships in much the same way that foreign-key relationships are declared in a relational database. For example, say you have a foreign-key relationship that you can't express using a simple parent-child relationship in our XML. You can declare the two related elements as in Listing 3:
<xsd:element name="rootElement">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="elementOne" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="elementOneKey" type="integer" />
<xsd:attribute name="elementOneDesc" type="text" />
</xsd:complexType>
<xsd:key name="elementOnePK">
<xsd:selector xpath=".//elementOne"/>
<xsd:field xpath="@elementOneKey"/>
</xsd:key>
</xsd:element>
<xsd:element name="elementTwo" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="elementTwoKey" type="integer" />
<xsd:attribute name="elementOneKey" type="integer" />
<xsd:attribute name="elementTwoDesc" type="text" />
</xsd:complexType>
<xsd:keyref name="elementOneFK" refer="elementOnePK">
<xsd:selector xpath=".//elementTwo"/>
<xsd:field xpath="@elementOneKey"/>
</xsd:keyref>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
哪个更好,因为您可以正确链接 ID 和引用.
Which is even better as you can link id and reference properly.
有加分我正在学习东西.
Have a plus I'm learning stuff.
这篇关于XSD 限制规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!