XSD不同序列中元素的唯一约束

时间:2017-02-22 11:37:34

标签: xml xsd unique

我遵循xsd架构文件:

   <?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xsd:element name="Config">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Modul">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element maxOccurs="unbounded" name="Content1">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element maxOccurs="unbounded" name="Content2">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="AttributeType1">
                            <xsd:complexType>
                              <xsd:attribute name="X" type="xsd:double" use="required" />
                            </xsd:complexType>
                          </xsd:element>
                          <xsd:element name="AttributeType1">
                            <xsd:complexType>
                              <xsd:attribute name="X" type="xsd:double" use="required" />
                            </xsd:complexType>
                          </xsd:element>
                        </xsd:sequence>
                        <xsd:attribute name="Name" type="xsd:string" use="required" />
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                  <xsd:attribute name="Name" type="xsd:string" use="required" />
                </xsd:complexType>
                <xsd:unique name="Content2NameUniqueKey">
                  <xsd:selector xpath="Content2" />
                  <xsd:field xpath="@Name" />
               </xsd:unique>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="Name" type="xsd:string" use="required" />
           </xsd:complexType>
          <xsd:unique name="Content1NameUniqueKey">
            <xsd:selector xpath="Content1" />
            <xsd:field xpath="@Name" />
          </xsd:unique>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xs:schema>

使用唯一约束,我可以检查content1名称是否唯一,以及content1元素中是否所有content2 name都是unuique。

但我的问题是,我想检查所有content2元素中是否所有content2元素都是唯一的。因此,只能有一个名为&#34; xy&#34;的content1元素。在完整的XML文件中。

有人可以帮助我吗?

向所有人致以问候!

1 个答案:

答案 0 :(得分:1)

Content2的唯一元素只需在Modul中移动一级,使用XPath Content1/Content2。这样,Content2元素在整个Modul中也必须是唯一的。

AttributeType1的双重问题还有另一个问题,我将其替换为min/maxOccurs=2,但它可能只是一个错字。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema"     xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xsd:element name="Config">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Modul">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element maxOccurs="unbounded" name="Content1">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element maxOccurs="unbounded" name="Content2">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="AttributeType1" maxOccurs="2" minOccurs="2">
                            <xsd:complexType>
                              <xsd:attribute name="X" type="xsd:double" use="required" />
                            </xsd:complexType>
                          </xsd:element>
                        </xsd:sequence>
                        <xsd:attribute name="Name" type="xsd:string" use="required" />
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                  <xsd:attribute name="Name" type="xsd:string" use="required" />
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="Name" type="xsd:string" use="required" />
          </xsd:complexType>
          <xsd:unique name="Content1NameUniqueKey">
            <xsd:selector xpath="Content1" />
            <xsd:field xpath="@Name" />
          </xsd:unique>
          <xsd:unique name="Content2NameUniqueKey">
            <xsd:selector xpath="Content1/Content2" />
            <xsd:field xpath="@Name" />
          </xsd:unique>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xs:schema>