我在模式中有一个xs:unique声明。工作得很好。但是当我替换一个元素是关键时,它就不再起作用了。
是否有什么东西可以确保唯一的键在替换中持续存在?
例如,我有一个xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<el id="1"/>
<el id="2"/>
</root>这个模式是:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="typeel">
<xs:attribute name="id"/>
</xs:complexType>
<xs:element name="el" type="typeel"/>
<xs:element name="root">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element ref="el"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="idgoooood">
<xs:selector xpath="el"/>
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
</xs:schema>干得很好。
但是,如果我添加模式:
<xs:element name="el-bis" type="typeel" substitutionGroup="el"/>我可以编写我的xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<el id="1"/>
<el id="2"/>
<el-bis id="3"/>
</root>非常好。但不幸的是,我也能写到:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<el id="1"/>
<el id="2"/>
<el-bis id="2"/>
</root>我可不想那样。我应该让唯一的钥匙在替换中保持不变..。有可能吗?如果没有,有什么解决办法?
谢谢。
发布于 2011-11-14 09:57:16
我认为这不可能。您必须将唯一声明复制到替换元素。
发布于 2019-10-31 15:17:56
这可能是我回答过的最古老的问题,因此我相当肯定你不再关心一个解决方案,但如果像我这样的人来找答案的话,你就来了……
您是关闭的,您只需要更改唯一约束中的选择器,以匹配所有(而不仅仅是el ),那么无论在substitutionGroup中使用哪个元素,该约束都将被应用。
<xs:unique name="idgoooood">
<xs:selector xpath="*"/>
<xs:field xpath="@id"/>
</xs:unique>全套Xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="typeel">
<xs:attribute name="id"/>
</xs:complexType>
<xs:element name="el" type="typeel"/>
<xs:element name="el-bis" type="typeel" substitutionGroup="el"/>
<xs:element name="root">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element ref="el"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="idgoooood">
<xs:selector xpath="*"/>
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
</xs:schema>示例Xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<el id="1"/>
<el id="2"/>
<el-bis id="2" /> <!-- Fails due to duplication -->
</root>https://stackoverflow.com/questions/4475000
复制相似问题