2016-08-22 48 views
0

定义XML元素作为唯一我有以下关联的XSD的XML文件:使用XSD

元素“名”的复杂类型“UpgraderConfig”必须是唯一的。 我已将“unique”标记添加到“UpgraderConfigContainer”定义中,但似乎无法获得所需的行为。

任何人都可以请告诉我我做错了什么?

<?xml version="1.0"?> 
<xs:schema targetNamespace="http://www.hp.com/maas/1.0.0/UpgraderConfig" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:config="http://www.hp.com/maas/1.0.0/UpgraderConfig" 
      elementFormDefault="qualified" 
      version="1.0.0"> 

    <xs:simpleType name="stringWithoutWhiteSpace"> 
     <xs:annotation> 
      <xs:documentation>One or more occurrences of any character which is not whitespace (i.e. not space, tab, newline, return)</xs:documentation> 
     </xs:annotation> 
     <xs:restriction base="xs:string"> 
      <xs:minLength value="1" /> 
      <xs:pattern value="\S+" /> 
     </xs:restriction> 
    </xs:simpleType> 


    <xs:complexType name="UpgraderConfigContainer"> 
     <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
      <xs:element name="Upgrader" type="config:UpgraderConfig" > 
       <xs:unique name="uniqueUgraderName"> 
        <xs:selector xpath="Upgrader"/> 
        <xs:field xpath="name"/> 
       </xs:unique> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="UpgraderConfig"> 
     <xs:sequence> 
      <xs:element name="name" type="xs:string"/> 
      <xs:element name="className" type="config:stringWithoutWhiteSpace"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:element name="UpgraderConfigs" type="config:UpgraderConfigContainer" /> 

</xs:schema> 

回答

1

您已将独特约束定义得太低。它本身的一个元素不可能是唯一的。只有包含它的容器才能有唯一的约束。您已将UpgraderConfigContainer标记为小标题,因为它不是容器。相反,它是元素,它是未绑定的,这意味着您有零个或多个UpgraderConfigContainer s。包含UpgraderConfigContainer的元素必须具有唯一约束。您必须将其添加到UpgraderConfigs

<?xml version="1.0"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:config="http://www.hp.com/maas/1.0.0/UpgraderConfig" targetNamespace="http://www.hp.com/maas/1.0.0/UpgraderConfig" elementFormDefault="qualified" version="1.0.0"> 
    <xs:simpleType name="stringWithoutWhiteSpace"> 
     <xs:annotation> 
      <xs:documentation>One or more occurrences of any character which is not whitespace (i.e. not space, tab, newline, return)</xs:documentation> 
     </xs:annotation> 
     <xs:restriction base="xs:string"> 
      <xs:minLength value="1"/> 
      <xs:pattern value="\S+"/> 
     </xs:restriction> 
    </xs:simpleType> 
    <xs:complexType name="UpgraderConfigContainer"> 
     <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
      <xs:element name="Upgrader" type="config:UpgraderConfig"/> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="UpgraderConfig"> 
     <xs:sequence> 
      <xs:element name="name" type="xs:string"/> 
      <xs:element name="className" type="config:stringWithoutWhiteSpace"/> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:element name="UpgraderConfigs" type="config:UpgraderConfigContainer"> 
     <xs:unique name="uniqueUpgraderName"> 
      <xs:selector xpath="config:Upgrader"/> 
      <xs:field xpath="config:name"/> 
     </xs:unique> 
    </xs:element> 
</xs:schema> 
+0

我不知道包含的元素只能对它有唯一的约束。谢谢,它确实有效。 –