在XSD中设置唯一的元素

时间:2013-10-24 09:41:40

标签: xml xsd

我有以下XSD,我需要添加另一个唯一约束。

我想在其父组元素中使Name唯一,但是每个WikiMeta中都可以重复一个组。

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="AllMeta">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="WikiMeta">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Subject" type="xs:string" minOccurs="1" maxOccurs="1" />
              <xs:element name="PageTitle" type="xs:string" minOccurs="1" maxOccurs="1" />
              <xs:element name="PageMetaDescription" type="xs:string" minOccurs="1" maxOccurs="1" />
              <xs:element name="Articles">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="WikiArticle" minOccurs="1" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1" />
                        </xs:sequence>
                      </xs:complexType>
                      <xs:unique name="uniqueFileName">
                        <xs:selector xpath="WikiMeta"/>
                        <xs:field xpath="FileName"/>
                      </xs:unique>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="Groups">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element maxOccurs="unbounded" name="WikiGroup">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="subject">
      <xs:selector xpath="WikiMeta"/>
      <xs:field xpath="Subject"/>
    </xs:unique>
    <xs:unique name="fileName">
      <xs:selector xpath="WikiMeta/Articles/WikiArticle"/>
      <xs:field xpath="FileName"/>
    </xs:unique>
  </xs:element>
</xs:schema>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

将约束添加到定义可见性级别的元素。这是修改后的XSD:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="AllMeta">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="WikiMeta">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Subject" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="PageTitle" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="PageMetaDescription" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="Articles">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="WikiArticle" minOccurs="1" maxOccurs="unbounded">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                                </xs:sequence>
                                            </xs:complexType>
                                            <xs:unique name="uniqueFileName">
                                                <xs:selector xpath="WikiMeta"/>
                                                <xs:field xpath="FileName"/>
                                            </xs:unique>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="Groups">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element maxOccurs="unbounded" name="WikiGroup">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                                <xs:key name="groupkey">
                                    <xs:selector xpath="WikiGroup"/>
                                    <xs:field xpath="Name"/>
                                </xs:key>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="subject">
            <xs:selector xpath="WikiMeta"/>
            <xs:field xpath="Subject"/>
        </xs:unique>
        <xs:unique name="fileName">
            <xs:selector xpath="WikiMeta/Articles/WikiArticle"/>
            <xs:field xpath="FileName"/>
        </xs:unique>
    </xs:element>
</xs:schema>

视觉上可能更容易理解:

enter image description here