遇到xsd:unique的问题

时间:2013-10-23 16:00:51

标签: xml xsd

我有以下XSD已经有一个独特的项目,但是我试图添加另一个没有成功。

我想在整个文档结构中使WikiArticle - Filename独一无二,但尽管存在多种不同的变体,但我无法让它失败。

有什么想法吗?

<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">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1" />
                        </xs:sequence>
                      </xs:complexType>
                    </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="uniqueSubject">
      <xs:selector xpath="WikiMeta"/>
      <xs:field xpath="Subject"/>
    </xs:unique>
  </xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:0)

鉴于我见过的其他模式,我冒昧地将WikiArticle的基数修改为无界(在你的情况下无关紧要)。

随后修改了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: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="uniqueSubject">
            <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>

看起来是什么样的(您现在可能想要添加其他约束,例如WikiGroup名称的唯一性)

enter image description here

有效的示例XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<AllMeta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <WikiMeta>
        <Subject>Subject1</Subject>
        <PageTitle>PageTitle1</PageTitle>
        <PageMetaDescription>PageMetaDescription1</PageMetaDescription>
        <Articles>
            <WikiArticle>
                <FileName>FileName1</FileName>
            </WikiArticle>
            <WikiArticle>
                <FileName>FileName2</FileName>
            </WikiArticle>
        </Articles>
        <Groups>
            <WikiGroup>
                <Name>Name1</Name>
            </WikiGroup>
            <WikiGroup>
                <Name>Name1</Name>
            </WikiGroup>
        </Groups>
    </WikiMeta>
    <WikiMeta>
        <Subject>Subject2</Subject>
        <PageTitle>PageTitle1</PageTitle>
        <PageMetaDescription>PageMetaDescription1</PageMetaDescription>
        <Articles>
            <WikiArticle>
                <FileName>FileName3</FileName>
            </WikiArticle>
            <WikiArticle>
                <FileName>FileName4</FileName>
            </WikiArticle>
        </Articles>
        <Groups>
            <WikiGroup>
                <Name>Name1</Name>
            </WikiGroup>
            <WikiGroup>
                <Name>Name1</Name>
            </WikiGroup>
        </Groups>
    </WikiMeta>
</AllMeta>

现在,将FileName4更改为FileName1,您将收到如下错误:

Error occurred while loading [], line 35 position 6
There is a duplicate key sequence 'FileName1' for the 'fileName' key or unique identity constraint.