2016-08-22 24 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

我不知道包含的元素只能對它有唯一的約束。謝謝,它確實有效。 –