Abstract types/multiple namespaces


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Abstract types/multiple namespaces

  1. #1
    Join Date
    Nov 2008
    Posts
    5

    Abstract types/multiple namespaces

    I have an XSD where I define an abstract base type. Other XSD files derive their types from the abstract base type. The base type defining XSD is in its own namespace, and the other XSD files have their own namespaces also. The problem I'm having is that the XML file which leverages the derived types won't recognize the derived types. Example:

    BaseType.xsd
    Code:
    <schema xmlns="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="unqualified"
            targetNamespace="urn:MyBaseType"
            xmlns:mbt="urn:MyBaseType">
    
      <complexType name="AbstractBaseType" abstract="true">
        <choice maxOccurs="unbounded">
          <element name="SomeElement">
            <complexType>
              <attribute name="Name"/>
            </complexType>
          </element>
        </choice>
      </complexType>
    
      <element name="ElementUsingAbstractType" type="mbt:AbstractBaseType"/>
    
    </schema>
    DerviedType.xsd
    Code:
    <schema xmlns="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="unqualified"
            xmlns:mbt="urn:MyBaseType"
            xmlns:mdt="urn:MyDerivedType"
            targetNamespace="urn:MyDerivedType">
      <import namespace="urn:MyBaseType" schemaLocation="BaseType.xsd"/>
    
      <complexType name="MyDerivedType">
        <complexContent>
          <extension base="mbt:AbstractBaseType">
            <sequence>
              <element name="SomeValue"></element>
            </sequence>
          </extension>
        </complexContent>
      </complexType>
    </schema>
    Test.xml:
    Code:
    <mbt:MyBaseType xmlns:mm="urn:MyBaseType"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="urn:MyBaseType BaseType.xsd
                  urn:MyDerivedType DerivedType.xsd">
    
      <ElementUsingAbstractType xsi:type="mdt:MyDerivedType" Name="Foo">
        <Value>Bar</Value>
      </ElementUsingAbstractType>
    
    </mbt:MyBaseType>
    Result from VS intellisense:
    "This is an invalid xsi:type 'mdt:MyDerivedType'"

    I've tried omitting the prefix to just xsi:type="MyDerivedType" in the XML too, though it doesn't work. There are no errors being reported by VS2008 in any of the XSD files, so I'm not sure what I'm doing wrong.
    Last edited by Aldel; 11-21-2008 at 05:38 PM.

  2. #2
    Join Date
    Nov 2008
    Posts
    1
    The problem is that you are validating the XML instance with the DerivedType, but you are keeping the root element from the BaseType.

    Infact the root element MyBaseType dowsnot exist at all.

    You will need to do the following changes in order to make the XML work:
    Code:
    <schema xmlns="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="unqualified"
            xmlns:mbt="urn:MyBaseType"
            xmlns:mdt="urn:MyDerivedType"
            targetNamespace="urn:MyDerivedType">
      <import namespace="urn:MyBaseType" schemaLocation="BaseType.xsd"/>
    
      <complexType name="MyDerivedType">
        <complexContent>
          <extension base="mbt:AbstractBaseType">
            <sequence>
              <element name="SomeValue"></element>
            </sequence>
          </extension>
        </complexContent>
      </complexType>
    
      <element name="MyBaseType" type="mdt:MyDerivedType"/>
    </schema>
    The mistake that you are trying to do is you are trying to get the root element into a subelement.

  3. #3
    Join Date
    Nov 2008
    Posts
    5
    Maybe it's best if I describe what I'm trying to do, since your answer wouldn't work for what I need. I appreciate the reply, don't get me wrong, and I see where the error is now that you pointed it out. I'm basically trying to emulate an abstract method in an OOP class. I would like to have the element in a type be an abstract that can be any number of different derived types. Here's an example of what I'd like:

    Code:
    <Root>
      <SomeElement>
        <SomeElementOfDerivedType>
          <ChildElementOne>Some data.</ChildElementOne>
          <ChildElementTwo>More data.</ChildElementTwo>
        </SomeElementOfDerivedType>
        <AnotherElementOfDerivedType>
          <ChildElementA>Foo</ChildElementA>
          <ChildElementB>Bar</ChildElementB>
          <ChildElementC>Other uninteresting data</ChildElementC>
        </AnotherElementOfDerivedType>
      </SomeElement>
    </Root>
    The goal here is to provide a schema in XSD that would give the person writing the XML some guidance as to what are valid child nodes of SomeElement, and provide further information as the valid nodes for each child node of the elements which derive from the same base type. This is key: each of the nodes under SomeElement need to be derived from the same base type. The example omits many fields and attributes that are contained in the "Root" and "SomeElement" elements as well as the "ComplexTypeWithAbstractTypeInside" type.

    I figured the best way to accomplish the goal was with something that looked like this:

    Code:
    <complexType name="AbstractType" abstract="true"/>
    
    <complexType name="ComplexTypeWithAbstractTypeInside">
      <choice maxOccurs="unbounded">
        <element name="ElementWithAbstractType" type="AbstractType"/>
      </choice>
    </complexType>
    
    <element name="Root">
      <complexType>
        <sequence>
          <element name="SomeElement">
            <complexType>
              <sequence>
                <element name="MyElement" type="ComplexTypeWithAbstractTypeInside"/>
              </sequence>
            </complexType>
          </element>
        </sequence>
      </complexType>
    </element>
    I'd then have seperate XSD files that housed one or more types that derived from AbstractType. I have no idea if this is possible, but I sure hope it is.
    Last edited by Aldel; 11-24-2008 at 12:25 PM.

Similar Threads

  1. Replies: 7
    Last Post: 01-16-2006, 05:04 PM
  2. Comparison methods
    By ericelysia1 in forum Java
    Replies: 34
    Last Post: 05-15-2005, 07:39 PM
  3. abstract methods/polymorphism
    By Mark Destrampe in forum Java
    Replies: 1
    Last Post: 11-05-2002, 07:31 PM
  4. COM+ SOAP 2.0 and Abstract Classes
    By Constant in forum VB Classic
    Replies: 0
    Last Post: 07-05-2002, 04:37 PM
  5. Interfaces and abstract classes
    By Jason Bock in forum .NET
    Replies: 6
    Last Post: 02-02-2001, 03:49 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