我已经看到很多示例展示了如何在xsd架构中使用unique元素,但我没有成功让它适合我。 我使用http://www.freeformatter.com/xml-validator-xsd.html测试的错误(如果您知道更好的测试工具让我知道)是:"'架构的内容'是无效的。元素'独特'无效,错位或太频繁发生。"
XSD:
<xsd:schema xmlns:inf="http://www.example.net/inflation" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:instrument="http://www.example.net/Instrument" targetNamespace="http://www.example.net/inflation" elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:import namespace="http://www.example.net/Instrument" schemaLocation="instrument.xsd"/>
<xsd:element name="inflations" type="inf:Inflations"/>
<xsd:complexType name="Inflations">
<xsd:sequence>
<xsd:element name="historyTable" type="xsd:string" default="INFLATION_HISTORY"/>
<xsd:element name="inflationHistory" type="inf:InflationHistory" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="InflationHistory">
<xsd:sequence>
<xsd:element name="identifier" type="integer"/>
<xsd:element name="inflation" type="inf:Inflation" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!-- Uniqueness -->
<xsd:unique name="CodeUniqueKey">
<xsd:selector xpath="inf:inflationHistory" />
<xsd:field xpath="inf:identifier" />
</xsd:unique>
</xsd:schema>
答案 0 :(得分:1)
唯一约束必须进入元素声明,所以:
<xsd:element name="inflations" type="inf:Inflations>
<xsd:unique name="CodeUniqueKey">
...
</xsd:unique>
</xsd:element>