0
<!--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:

<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

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

1
  • Element IDs should be unique. Commented Feb 19, 2012 at 14:26

2 Answers 2

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: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.

Sign up to request clarification or add additional context in comments.

4 Comments

they problem with this solution is that you actually want duplicated Classes, but only if they are in different Packages.
i used key and keyref but now i have problem to refer their name cause ; i have 2 scope on Class Name and dont know how to refer 2 key on 1 keyref
@gioele Updated for your comment. I didn't see the package/class linkage, but I guessed the missing OwnedType element is like TypedElement and has a Package attribute.
@gezgin I updated the answer to show using two fields, in case that's what you're getting at regarding "2 scopes".
0

What do you mean with ID? You say

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

but in your code you have

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

is lib:Name defined as 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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.