xml xs:unique的问题

时间:2013-04-11 15:35:39

标签: c# xml xsd schema unique

我的xs:unique不起作用。我尝试了本网站提供的各种解决方案。他们似乎都不适合我。我正在使用C#来验证我的xml对模式。例如,密钥passwordomni被重复。

以下是我的架构。

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mstns="http://tempuri.org/SqlManifest.xsd">
  <xs:element name="sqlmanifest">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="sqlparameters" maxOccurs="1" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="sqlparameter" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute type="xs:string" name="key" use="required"/>
                      <xs:attribute type="xs:string" name="description" use="required"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
                <xs:unique name="unique-key">
                  <xs:selector xpath ="mstns:sqlparameter"/>
                  <xs:field xpath="@key"/>
                </xs:unique>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="sqlcommands" maxOccurs="1" minOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="sqlcommand" maxOccurs="unbounded" minOccurs="1">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute type="xs:string" name="path" use="required"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
                <xs:unique name="unique-path">
                  <xs:selector xpath ="mstns:sqlcommand"/>
                  <xs:field xpath="@path"/>
                </xs:unique>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute type="xs:boolean" name="transactional"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

下面是我的xml

<sqlmanifest transactional="false">
    <sqlparameters>
        <sqlparameter key="dbpath" description="Please enter dbpath"/>
        <sqlparameter key="passworduserOmni" description="Please enter password for OmniUser"/>
        <sqlparameter key="passwordadminOmni" description="Please enter password for OmniAdminUser"/>
                <sqlparameter key="passwordadminOmni" description="Please enter password for OmniAdminUser"/>
    </sqlparameters>
    <sqlcommands>
        <sqlcommand path="scripts/script1.sql"/> 
        <sqlcommand path="scripts/script2.sql" />
        <sqlcommand path="scripts/scripts3.sql" />
        <sqlcommand path="scripts/scripts4.sql" />  
                <sqlcommand path="scripts/script1.sql"/> 
    </sqlcommands>
</sqlmanifest>

1 个答案:

答案 0 :(得分:2)

您的XML不使用名称空间,因此您的XSD应该没有目标名称空间,因此您不得在XPath(字段/选择器)中使用任何前缀。

约束必须放在适当的水平。因此,unique-key必须移至sqlparameters下的unique-pathsqlcommands下。

您更正的XSD如下:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mstns="http://tempuri.org/SqlManifest.xsd">
    <xs:element name="sqlmanifest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="sqlparameters" maxOccurs="1" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="sqlparameter" maxOccurs="unbounded" minOccurs="0">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute type="xs:string" name="key" use="required"/>
                                            <xs:attribute type="xs:string" name="description" use="required"/>
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                    <xs:unique name="unique-key">
                        <xs:selector xpath ="sqlparameter"/>
                        <xs:field xpath="@key"/>
                    </xs:unique>
                </xs:element>
                <xs:element name="sqlcommands" maxOccurs="1" minOccurs="1">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="sqlcommand" maxOccurs="unbounded" minOccurs="1">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute type="xs:string" name="path" use="required"/>
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                    <xs:unique name="unique-path">
                        <xs:selector xpath ="sqlcommand"/>
                        <xs:field xpath="@path"/>
                    </xs:unique>
                </xs:element>
            </xs:sequence>
            <xs:attribute type="xs:boolean" name="transactional"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

如果您在前后可视化约束,它将帮助您理解为什么范围应该是它的方式;另外,考虑到选择器xpath以元素为根,它根据您的期望不匹配。

enter image description here