如何在不同范围内定义具有相同唯一名称属性的2个不同元素,并将其名称引用到属性

时间:2022-04-13 23:54:28
<!--Metamodel (root element)-->
  <xsd:element name="Metamodel">
    <xsd:complexType>
      <xsd:choice minOccurs="0" maxOccurs="unbounded">
        <xsd:element ref="Package"/>
        <xsd:element ref="EnumerationLiteral"/>
        <xsd:element ref="Class"/>
        <xsd:element ref="Operation"/>
        <xsd:element ref="Parameter"/>
        <xsd:element ref="Property"/>
        <xsd:element ref="PrimitiveType"/>
        <xsd:element ref="Enumeration"/>
      </xsd:choice>
    </xsd:complexType>
  </xsd:element>
  <!--OBJECT-->
  <xsd:complexType name="Object"  abstract="true"/>
  <!--ELEMENT-->
  <xsd:complexType name="Element" abstract="true">
    <xsd:complexContent>
      <xsd:extension base="Object">
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <!--NAMED ELEMENT-->
  <xsd:complexType name="NamedElement" abstract="true">
    <xsd:complexContent>
      <xsd:extension base="Element">
        <xsd:attribute name="Name" type="xsd:ID" use="optional"/>
        <xsd:attribute name="Description" type="lib:Description" use="optional"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <!--TYPE-->
  <xsd:complexType name="Type" abstract="true">
    <xsd:complexContent>
      <xsd:extension base="NamedElement">
        <xsd:attribute name="Package" type="lib:Package" use="optional"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <!--TYPED ELEMENT-->
  <xsd:complexType name="TypedElement" abstract="true">
    <xsd:complexContent>
      <xsd:extension base="NamedElement">
        <xsd:attribute name="Type" use="optional"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <!--PACKAGE-->
  <xsd:element name="Package">
    <!--PACKAGE TYPE-->
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="NamedElement">
          <xsd:all>
            <xsd:element ref="OwnedType" minOccurs="0"/>
            <xsd:element ref="NestedPackage" minOccurs="0"/>
          </xsd:all>
          <xsd:attribute name="NestingPackage" type="lib:NestingPackage"/>
          <xsd:attribute name="Uri" type="lib:Uri" use="optional"/>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>
  <!--ENUMERATIONAL LITERAL-->
  <xsd:element name="EnumerationLiteral" type="EnumerationLiteralType"/>
  <!--ENUMERATION LITERAL TYPE-->
  <xsd:complexType name="EnumerationLiteralType">
    <xsd:complexContent>
      <xsd:extension base="NamedElement">
        <xsd:attribute name="Enumeration" type="lib:Enumeration" use="optional"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <!--CLASS-->
  <xsd:element name="Class" type="ClassType"/>
  <!--CLASS TYPE-->
  <xsd:complexType name="ClassType">
    <xsd:complexContent>
      <xsd:extension base="Type">
        <xsd:all>
          <xsd:element ref="OwnedOperation" minOccurs="0"/>
          <xsd:element ref="OwnedAttribute" minOccurs="0"/>
          <xsd:element ref="SuperClass" minOccurs="0"/>
        </xsd:all>
        <xsd:attribute name="IsAbstract" type="lib:IsAbstract"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

I have Name attribute on NamedElement element and this type is ID. Thats ok but i can't do:

我在NamedElement元素上有Name属性,这个类型是ID。多数民众赞成,但我做不到:

<Metamodel>
  <Package Name="Serhat">
  </Package>
  <Class Name="Serhat"></Class>
</Metamodel>

There is a error i cant do this but i need to do this.

有一个错误,我不能这样做,但我需要这样做。

I need only one Package with Name="Serhat" but Class may be have Nmae="Serhat" and

我只需要一个名为“Serhat”的包,但Class可能有Nmae =“Serhat”和

another class in another package may have Name="Serhat" but this code can't do this. How can i do this any idea ?

另一个包中的另一个类可能有Name =“Serhat”,但是这段代码不能这样做。我怎么能这样做呢?

2 个解决方案

#1


1  

xsd:ID implies a global uniqueness constraint. If you want a uniqueness constraint that's not global, try adding a xsd:unique (MSDN example) identity constraint to the containing element(s), which might be the doc root element.

xsd:ID表示全局唯一性约束。如果您想要一个非全局的唯一性约束,请尝试将xsd:unique(MSDN示例)标识约束添加到包含元素(可能是doc根元素)。

<xsd:element name="Metamodel">
   ....
   <xs:unique name="uniquePackageNames">
     <xs:selector xpath="Package"/>
     <xs:field xpath="@Name"/>
   </xs:unique>
   <xs:unique name="uniqueClassNames">
     <xs:selector xpath="Class"/>
     <xs:field xpath="@Name"/>
     <xs:field xpath="@Package"/>
   </xs:unique>
</xsd:element>

Edit: Added Class/@Package field so that class name uniqueness is only within a package.

编辑:添加了Class / @ Package字段,以便类名唯一性仅在包中。

#2


0  

What do you mean with ID? You say

你对ID有什么意思?你说

I have Name attribute on NamedElement element and this type is ID

我在NamedElement元素上有Name属性,这个类型是ID

but in your code you have

但是在你的代码中你有

<xsd:attribute name="Name" type="lib:Name" use="optional"/>

is lib:Name defined as xsd:ID?

是lib:名称定义为xsd:ID?

The meaning if xsd:ID is that you cannot have another element or name with such ID in the same document. If you want a more sophisticated method to define which ID are possible and how they can mix, you must use xsd:NCName as the type for the Name attribute and additional Schematron rules.

xsd:ID的含义是您不能在同一文档中使用具有此ID的其他元素或名称。如果您需要更复杂的方法来定义哪些ID可能以及它们如何混合,则必须使用xsd:NCName作为Name属性的类型和其他Schematron规则。

Schematron is used to express rules that cannot be expressed in XML Schema, in your case to say that different Packages must have different Name attributes but that Classes in different Package can have the same name.

Schematron用于表示无法在XML Schema中表达的规则,在您的情况下,可以说不同的Packages必须具有不同的Name属性,但不同Package中的Classes可以具有相同的名称。

#1


1  

xsd:ID implies a global uniqueness constraint. If you want a uniqueness constraint that's not global, try adding a xsd:unique (MSDN example) identity constraint to the containing element(s), which might be the doc root element.

xsd:ID表示全局唯一性约束。如果您想要一个非全局的唯一性约束,请尝试将xsd:unique(MSDN示例)标识约束添加到包含元素(可能是doc根元素)。

<xsd:element name="Metamodel">
   ....
   <xs:unique name="uniquePackageNames">
     <xs:selector xpath="Package"/>
     <xs:field xpath="@Name"/>
   </xs:unique>
   <xs:unique name="uniqueClassNames">
     <xs:selector xpath="Class"/>
     <xs:field xpath="@Name"/>
     <xs:field xpath="@Package"/>
   </xs:unique>
</xsd:element>

Edit: Added Class/@Package field so that class name uniqueness is only within a package.

编辑:添加了Class / @ Package字段,以便类名唯一性仅在包中。

#2


0  

What do you mean with ID? You say

你对ID有什么意思?你说

I have Name attribute on NamedElement element and this type is ID

我在NamedElement元素上有Name属性,这个类型是ID

but in your code you have

但是在你的代码中你有

<xsd:attribute name="Name" type="lib:Name" use="optional"/>

is lib:Name defined as xsd:ID?

是lib:名称定义为xsd:ID?

The meaning if xsd:ID is that you cannot have another element or name with such ID in the same document. If you want a more sophisticated method to define which ID are possible and how they can mix, you must use xsd:NCName as the type for the Name attribute and additional Schematron rules.

xsd:ID的含义是您不能在同一文档中使用具有此ID的其他元素或名称。如果您需要更复杂的方法来定义哪些ID可能以及它们如何混合,则必须使用xsd:NCName作为Name属性的类型和其他Schematron规则。

Schematron is used to express rules that cannot be expressed in XML Schema, in your case to say that different Packages must have different Name attributes but that Classes in different Package can have the same name.

Schematron用于表示无法在XML Schema中表达的规则,在您的情况下,可以说不同的Packages必须具有不同的Name属性,但不同Package中的Classes可以具有相同的名称。