Suppose we have the following schema:
假设我们有以下架构:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="a_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="a_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="b_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="b_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="c_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="ref" type="xs:IDREF" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
and here's the sample xml file:
这是示例xml文件:
<root>
<a_elements>
<a_element id="id1"/>
<a_element id="id2"/>
</a_elements>
<b_elements>
<b_element id="id3"/>
<b_element id="id4"/>
</b_elements>
<c_elements>
<c_element id="id5" ref="id1"/>
<c_element id="id6" ref="id2"/>
</c_elements>
</root>
So that c_elements can reference a_elements and b_elements by id. Is it possible to somehow restrict ref attribute to only accept references to elements from one group, say a_elements?
因此c_elements可以通过id引用a_elements和b_elements。有可能以某种方式限制ref属性只接受来自一个组的元素的引用,例如a_elements吗?
3 个解决方案
#1
7
Further to my earlier answer, in theory you can't restrict using purely ID/IDREF however it's possible to add an identity constraint which fulfil your requirement:
继我之前的回答,理论上你不能限制使用纯粹的ID / IDREF,但是可以添加满足你要求的身份约束:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="a_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="a_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="b_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="b_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="c_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="ref" type="xs:IDREF" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:keyref name="theKeyRef" refer="theKey">
<xs:selector xpath="c_elements/*"/>
<xs:field xpath="@ref"/>
</xs:keyref>
<xs:key name="theKey">
<xs:selector xpath="a_elements/*"/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
</xs:schema>
#2
1
I'm not aware of any mechanism to do this using ID and IDREF. By design ID and IDREF refer to all tags in the document.
我不知道使用ID和IDREF执行此操作的任何机制。按设计ID和IDREF引用文档中的所有标记。
That said, you could work around this in some way - perhaps with validation rules on whatever processes the data structure. It would be fairly easy to do this using Xpath expressions for example. You could certainly achieve this using a Schematron assertion. There's an example of this here: http://zvon.org/xxl/SchematronTutorial/Examples/Example16/example.html
也就是说,您可以通过某种方式解决这个问题 - 可能在数据结构的任何进程上都有验证规则。例如,使用Xpath表达式执行此操作相当容易。你当然可以使用Schematron断言来实现这一点。这里有一个例子:http://zvon.org/xxl/SchematronTutorial/Examples/Example16/example.html
Hope this helps.
希望这可以帮助。
Ken
肯
#3
0
Solution given by kennethmay may not work if you are using XSD 1.0. For example, I am using visual studio 2015 editor and pointing to let's say b's element (e.g. ) is not identified as an error. I guess this works only for XSD version 1.1
如果您使用XSD 1.0,kennethmay给出的解决方案可能无效。例如,我正在使用visual studio 2015编辑器并指向让我们说b的元素(例如)未被识别为错误。我想这只适用于XSD 1.1版
#1
7
Further to my earlier answer, in theory you can't restrict using purely ID/IDREF however it's possible to add an identity constraint which fulfil your requirement:
继我之前的回答,理论上你不能限制使用纯粹的ID / IDREF,但是可以添加满足你要求的身份约束:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="a_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="a_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="b_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="b_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="c_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="ref" type="xs:IDREF" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:keyref name="theKeyRef" refer="theKey">
<xs:selector xpath="c_elements/*"/>
<xs:field xpath="@ref"/>
</xs:keyref>
<xs:key name="theKey">
<xs:selector xpath="a_elements/*"/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
</xs:schema>
#2
1
I'm not aware of any mechanism to do this using ID and IDREF. By design ID and IDREF refer to all tags in the document.
我不知道使用ID和IDREF执行此操作的任何机制。按设计ID和IDREF引用文档中的所有标记。
That said, you could work around this in some way - perhaps with validation rules on whatever processes the data structure. It would be fairly easy to do this using Xpath expressions for example. You could certainly achieve this using a Schematron assertion. There's an example of this here: http://zvon.org/xxl/SchematronTutorial/Examples/Example16/example.html
也就是说,您可以通过某种方式解决这个问题 - 可能在数据结构的任何进程上都有验证规则。例如,使用Xpath表达式执行此操作相当容易。你当然可以使用Schematron断言来实现这一点。这里有一个例子:http://zvon.org/xxl/SchematronTutorial/Examples/Example16/example.html
Hope this helps.
希望这可以帮助。
Ken
肯
#3
0
Solution given by kennethmay may not work if you are using XSD 1.0. For example, I am using visual studio 2015 editor and pointing to let's say b's element (e.g. ) is not identified as an error. I guess this works only for XSD version 1.1
如果您使用XSD 1.0,kennethmay给出的解决方案可能无效。例如,我正在使用visual studio 2015编辑器并指向让我们说b的元素(例如)未被识别为错误。我想这只适用于XSD 1.1版