0

我有以下 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>
4

1 回答 1

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 名称的唯一性)

在此处输入图像描述

一个有效的示例 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.
于 2013-10-23T16:12:14.440 回答