[xsd-users] Hierarchies and code generation.

Ivan Le Lann ivan.lelann at free.fr
Wed Mar 2 04:03:32 EST 2011


----- "Klaim - Joël Lamotte" <mjklaim at gmail.com> a écrit :

> Hi,
> 
> I'm having troubles working on an xsd definition to generate an
> XML-format
> language parser in C++.
> 
> Here is what I want to have in the end in C++ (in a specific
> namespace) :
> 
>  1. an abstract class Object. It's a common interface.
>  2. child classes of Object : Object_Unknown, Object_Video,
> Object_Sprite,
> Object_Audio, Object_Sprite
>  3. an Object should have a "list" of children Object objects. As
> Element is
> abstract, the children should be of Object child class types (2.).
>  4. I still want to use the specific xml interface of the child types
> in the
> "list" element :
> 
> <object id="my_object" >
>     <children>
>         <object ... />
>         <sprite > <position x="1" y="2" /> </sprite> <--- specific
> interface
> of a child type
>     </children>
> </object>
> 
> 
> 
> To achieve all this I tried several variant of the following :
> 
> <xs:complexType name="object" abstract="true" ... >
> 
>     ...blahblah
>     <xs:sequence>
>          <xs:element name="children" type="list_object" />
>     </xs:sequence>
> 
> 
> </xs:complexType>
> 
> child element types...
> <xs:complexType name="object_unknown" ... />
> <xs:complexType name="object_sprite" ... />
> <xs:complexType name="object_audio" ... />
> 
> etc.
> 
> <xs:complexType name="list_object">
> 
>    <xs:sequence maxOccur="unbouded" minOccur="0" >
>        <xs:element name="object" type="object_unknown" />
>        <xs:element name="sprite" type="object_sprite" />
>        <xs:element name="audio" type="object_audio" />
>         etc.
>    </xs:sequence>
> 
> 
> But the generated C++ code of list_object will never expose a sequence
> of
> "object" types, using
> the common interface to list all the object instances without knowing
> the
> child type.

Indeed. I fail to see how it could when that information is not in the schema.

> So, is there a way to make my list_object provide a sequence of
> "object"?
> 

See the schema taken from XSD/e Hybrid  "polymorphic" example below.
I have no XSD distribution now on my computer, but I guess a similar example 
is in there too.

Regards,
Ivan

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:complexType name="person">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <!-- substitution group root -->
  <xsd:element name="person" type="person"/>


  <xsd:complexType name="superman">
    <xsd:complexContent>
      <xsd:extension base="person">
        <xsd:attribute name="can-fly" type="xsd:boolean" use="required"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="superman" type="superman" substitutionGroup="person"/>

  <xsd:complexType name="batman">
    <xsd:complexContent>
      <xsd:extension base="superman">
        <xsd:attribute name="wing-span" type="xsd:unsignedInt" use="required"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:complexType name="supermen">
    <xsd:sequence>
      <xsd:element ref="person" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="supermen" type="supermen"/>

</xsd:schema>



More information about the xsd-users mailing list