无法为 xml 中的属性设置唯一约束
Can't make unique constraint for attribute in xml
我有 xml
如下文件:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="status_content_mapping.xsd">
<StateContentMappings>
<StateContentMapping>
<State id="1" name="main" />
<Content id="2" />
</StateContentMapping>
<StateContentMapping>
<State id="3" name="sign on" />
<Content id="1" />
</StateContentMapping>
<StateContentMapping>
<State id="3" name="sign on" />
<Content id="1" />
</StateContentMapping>
<StateContentMapping>
<State id="3" name="sign on" />
<Content id="1" />
</StateContentMapping>
</StateContentMappings>
</ROOT>
并且我需要使 State
元素的属性 id
在元素 StateContentMappings
中是唯一的。我使用 xsd 如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ROOT" type="RootType"/>
<xs:complexType name="RootType">
<xs:sequence>
<xs:element name="StateContentMappings" type="StateContentMappingsType" minOccurs="0">
<xs:unique name="StateIdIsUnique">
<xs:selector xpath="State" />
<xs:field xpath="@id" />
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="StateContentMappingsType">
<xs:sequence>
<xs:element name="StateContentMapping" type="StateContentMappingType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="StateContentMappingType">
<xs:sequence>
<xs:element name="State" type="StateType" minOccurs="1" maxOccurs="1" />
<xs:element name="Content" type="ContentType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="StateType">
<xs:attribute name="id" type="xs:int" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="ContentType">
<xs:attribute name="id" type="xs:int" use="required" />
</xs:complexType>
</xs:schema>
但出于某种原因,它在我的情况下不起作用(我使用 Eclipse 编辑器来检查 vakidation)。
在我看来,您的选择器元素正在寻找 StateContentMappings
的直接子元素 State
,并且有 none.
尝试使用
<xs:unique name="StateIdIsUnique">
<xs:selector xpath="StateContentMapping/State" />
<xs:field xpath="@id" />
</xs:unique>
我有 xml
如下文件:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="status_content_mapping.xsd">
<StateContentMappings>
<StateContentMapping>
<State id="1" name="main" />
<Content id="2" />
</StateContentMapping>
<StateContentMapping>
<State id="3" name="sign on" />
<Content id="1" />
</StateContentMapping>
<StateContentMapping>
<State id="3" name="sign on" />
<Content id="1" />
</StateContentMapping>
<StateContentMapping>
<State id="3" name="sign on" />
<Content id="1" />
</StateContentMapping>
</StateContentMappings>
</ROOT>
并且我需要使 State
元素的属性 id
在元素 StateContentMappings
中是唯一的。我使用 xsd 如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ROOT" type="RootType"/>
<xs:complexType name="RootType">
<xs:sequence>
<xs:element name="StateContentMappings" type="StateContentMappingsType" minOccurs="0">
<xs:unique name="StateIdIsUnique">
<xs:selector xpath="State" />
<xs:field xpath="@id" />
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="StateContentMappingsType">
<xs:sequence>
<xs:element name="StateContentMapping" type="StateContentMappingType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="StateContentMappingType">
<xs:sequence>
<xs:element name="State" type="StateType" minOccurs="1" maxOccurs="1" />
<xs:element name="Content" type="ContentType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="StateType">
<xs:attribute name="id" type="xs:int" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="ContentType">
<xs:attribute name="id" type="xs:int" use="required" />
</xs:complexType>
</xs:schema>
但出于某种原因,它在我的情况下不起作用(我使用 Eclipse 编辑器来检查 vakidation)。
在我看来,您的选择器元素正在寻找 StateContentMappings
的直接子元素 State
,并且有 none.
尝试使用
<xs:unique name="StateIdIsUnique">
<xs:selector xpath="StateContentMapping/State" />
<xs:field xpath="@id" />
</xs:unique>