XML Inheritance


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: XML Inheritance

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    XML Inheritance

    Hi,
    I'm having the following problem with XSD authoring.

    I want to have a type that includes an element.
    Then, I want to inherit from that type, and force the element inside it to be of more specific type.

    As I understand, this can be done using restriction derivation, but only if the specific element inside the deriving type is a restriction of the original element.
    Is there any idea how to derive from the original type and make the element in the deriving type an extesion of the element in the parent type?

    Here is an example, that shows what I want to achieve, but returns error on XMLSpy:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- edited with XMLSpy v2011 rel. 3 sp1 (http://www.altova.com) by Asi (Comm-IT) -->
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:complexType name="Covariance">
    <xs:sequence>
    <xs:element name="x" type="xs:double"/>
    <xs:element name="y" type="xs:double"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="ExtendedCovariance">
    <xs:complexContent>
    <xs:extension base="Covariance">
    <xs:sequence>
    <xs:element name="z" type="xs:double"/>
    </xs:sequence>
    </xs:extension>
    </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="Detection">
    <xs:sequence>
    <xs:element name="someElement" type="xs:string"/>
    <xs:element name="covariance" type="Covariance"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="RestrictedDetection">
    <xs:complexContent>
    <xs:restriction base="Detection">
    <xs:sequence>
    <xs:element name="someElement" type="xs:string"/>
    <xs:element name="covariance" type="ExtendedCovariance"/>
    </xs:sequence>
    </xs:restriction>
    </xs:complexContent>
    </xs:complexType>
    </xs:schema>

    The error message:
    Type RestrictedDetection (>def) is not a valid restriction of type Detection (>def).

    Does anyone have any idea how can I achive my goal?

  2. #2
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    49
    Unfortunately I am not sure how to accomplish this correctly, however, your error is self explanatory.

    "this can be done using restriction derivation, but only if the specific element inside the deriving type is a restriction of the original element"

    Your resultant type is not a restriction of the original type, it is an extension of it. If you were to limit or restrict the base type then this would be valid, but adding an element is an extension of the type.

    Work around:

    <xs:complexType name="ExtendedDetection">
    <xs:sequence>
    <xs:element name="someElement" type="xs:string"/>
    <xs:element name="covariance" type="ExtendedCovariance"/>
    </xs:sequence>
    </xs:complexType>
    Last edited by reedy837; 10-04-2011 at 04:34 AM.

  3. #3
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    49
    I have looked at this in more detail and what you need to use is abstraction instead.

    Reference:
    http://www.xfront.com/ElementHierarchy.html

    Example XSD:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:element name="Abstract" type="AbstractType" abstract="true" />
      <xs:complexType name="AbstractType" abstract="true">
        <xs:sequence>
          <xs:element name="x" type="xs:double"/>
          <xs:element name="y" type="xs:double"/>
        </xs:sequence>
      </xs:complexType>
      <xs:element name="Extension" type="ExtendedType" substitutionGroup="Abstract" />
      <xs:complexType name="ExtendedType">
        <xs:complexContent>
          <xs:extension base="AbstractType">
            <xs:sequence>
              <xs:element name="z" type="xs:double"/>
            </xs:sequence>
          </xs:extension>
        </xs:complexContent>
      </xs:complexType>
      <xs:complexType name="AbstractContainer">
        <xs:sequence>
          <xs:element name="someElement" type="xs:string" />
          <xs:element ref="Abstract" />
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="ExtendedAbstractContainer">
        <xs:complexContent>
          <xs:restriction base="AbstractContainer">
            <xs:sequence>
              <xs:element name="someElement" type="xs:string" />
              <xs:element ref="Extension" />
            </xs:sequence>
          </xs:restriction>
        </xs:complexContent>
      </xs:complexType>
    </xs:schema>

Similar Threads

  1. Try XML Junction
    By Tim in forum xml.announcements
    Replies: 0
    Last Post: 10-11-2001, 05:00 PM
  2. XML Startkabel Updates
    By xmlstartkabel in forum XML
    Replies: 0
    Last Post: 10-10-2001, 05:07 AM
  3. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 11:53 AM
  4. Check out Turbo XML: Free trial download
    By Extensibility in forum web.announcements
    Replies: 0
    Last Post: 07-20-2000, 07:20 PM
  5. Check out Turbo XML
    By Sales in forum xml.announcements
    Replies: 0
    Last Post: 07-20-2000, 07:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center