如何在xsd上定义属性的不同范围?

时间:2012-02-23 18:10:01

标签: xml xsd schema

<Metamodel>
  <Package Name="serhat">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>
  <Package Name="serhat2">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>
  <Class Name="serhat"></Class>
</Metamodel>

我对elements有name属性。这个name属性在Package元素中必须是唯一的 但在Class元素名称属性中可能会重复另一个类元素,如;

  <Package Name="serhat">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>
  <Package Name="serhat2">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>

我该如何定义这个属性?

1 个答案:

答案 0 :(得分:2)

我相信你想要的不是属性,而是身份约束。因此,生成的XSD看起来像这样(我假设Package下的Class必须以某种方式重复):

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Metamodel">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element maxOccurs="unbounded" name="Package">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="OwnedType">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element ref="Class" maxOccurs="unbounded"/>
                                        </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                        <xsd:attribute name="Name" type="xsd:string" use="required"/>
                    </xsd:complexType>
                    <xsd:unique name="ClassNameUniqueInPackage">
                        <xsd:selector xpath="OwnedType/Class"/>
                        <xsd:field xpath="@Name"/>
                        </xsd:unique>
                </xsd:element>
                <xsd:element ref="Class"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="Class">
        <xsd:complexType>
            <xsd:attribute name="Name" type="xsd:string" use="required"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

使用此假设模型,以下XML将验证:

<Metamodel>
    <Package Name="serhat">
        <OwnedType>
            <Class Name="serhat"></Class>
            <Class Name="serhat2"></Class>
        </OwnedType>
    </Package>
    <Package Name="serhat2">
        <OwnedType>
            <Class Name="serhat"></Class>
        </OwnedType>
    </Package>
    <Class Name="serhat"></Class>
</Metamodel>

但不是这样:

<Metamodel>
    <Package Name="serhat">
        <OwnedType>
            <Class Name="serhat"></Class>
            <Class Name="serhat"></Class>
        </OwnedType>
    </Package>
    <Package Name="serhat2">
        <OwnedType>
            <Class Name="serhat"></Class>
        </OwnedType>
    </Package>
    <Class Name="serhat"></Class>
</Metamodel>

错误是:

Error occurred while loading [], line 5 position 5
There is a duplicate key sequence 'serhat' for the 'ClassNameUniqueInPackage' key or unique identity constraint.