XML Polymorphism through inheritance?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Mayo

    #1

    XML Polymorphism through inheritance?

    Hi all,
    I've got a DTD which I'm trying to tidy up and improve into an XSD - I'm
    battling with an area of object orientation which I'm used to in Java, but
    unsure about whether I can do it in XML:

    I've got a container which currently contains a Choice element listing all
    of children it can contain. Whenever I add a new child I have to remember
    to extend this choice definition.

    Since all of the children are extensions of a single base-type I was hoping
    that there would be some way of defining that my container just contains
    multiple instances of the base-type (or it's children). This type of thing
    seems quite normal in OOP, I just don't know if I'm asking too much of XSDs.

    Cheers,
    Ian


  • C. M. Sperberg-McQueen

    #2
    Re: XML Polymorphism through inheritance?

    "Ian Mayo" <IanMayo@hotmai l.com> writes:
    [color=blue]
    > Hi all,
    > I've got a DTD which I'm trying to tidy up and improve into an XSD - I'm
    > battling with an area of object orientation which I'm used to in Java, but
    > unsure about whether I can do it in XML:
    >
    > I've got a container which currently contains a Choice element listing all
    > of children it can contain. Whenever I add a new child I have to remember
    > to extend this choice definition.
    >
    > Since all of the children are extensions of a single base-type I was hoping
    > that there would be some way of defining that my container just contains
    > multiple instances of the base-type (or it's children). This type of thing
    > seems quite normal in OOP, I just don't know if I'm asking too much of XSDs.[/color]

    Yes. Define your container type as containing 1 occurrence
    (or however many you want) of top-level element foo, of type Foo.

    Now for each of the possible kinds of children, specify that
    they are in the substitution group of element foo.

    E.g. <xs:element name="bar" type="my:Foo" substitutionGro up="my:foo"/>
    <xs:element name="baz" type="my:Foo2" substitutionGro up="my:foo"/>

    Constraints:
    - the 'foo' element needs to be top-level, not local (so
    that other element declarations can point at it and say
    "That's my substitution-group head over there")
    - the type of each child needs to be derived from type Foo
    (you mention that this is the case, so it shouldn't be
    a problem)

    In some cases, you may wish to make the foo element itself
    abstract, so that it cannot be instantiated (so only my:bar
    and my:baz can occur, not my:foo).

    I hope this helps.

    -C. M. Sperberg-McQueen
    World Wide Web Consortium


    Comment

    • Ian Mayo

      #3
      Re: XML Polymorphism through inheritance?

      A thousand thank-you's Michael.

      I can't admit to completely understading the answer, but now I know to
      investigate Substitution Groups in my O'Reilly XML Schema book - especially
      now I know it certainly is "do-able".

      Thanks again,
      Ian Mayo

      "C. M. Sperberg-McQueen" <cmsmcq@acm.org > wrote in message
      news:873cgoj4pd .fsf@acm.org...[color=blue]
      > "Ian Mayo" <IanMayo@hotmai l.com> writes:
      >
      >
      > I hope this helps.
      >
      > -C. M. Sperberg-McQueen
      > World Wide Web Consortium
      >
      >[/color]


      Comment

      • Eric Sirois

        #4
        Re: XML Polymorphism through inheritance?

        Hello Ian,

        If you want to use the globally defined elements <A> and <B>, change the
        following content model

        <xs:complexTy pe name="ListTypeB ">
        <xs:choice minOccurs="0" maxOccurs="unbo unded">
        <xs:element name="A" type="TypeA"/>
        <xs:element name="B" type="TypeB"/>
        </xs:choice>
        </xs:complexType>

        to:

        <xs:complexTy pe name="ListTypeB ">
        <xs:choice minOccurs="0" maxOccurs="unbo unded">
        <xs:element ref="A" />
        <xs:element ref="B" />
        </xs:choice>
        </xs:complexType>

        There is a difference bettwen the two content models. The top defines
        and use local element <A> and <B>. They are not the same as the two
        global element defintion. Think of it as defining a local variable in
        a method with the same name as global variable in a class.

        Kind regards,
        Eric


        Ian Mayo wrote:[color=blue]
        > Thanks again for that advice Michael.
        >
        > It's worked fine for defining my structure to model "the real world".
        >
        > Unfortunately my XML editor (XMLSpy) doesn't appear to be property
        > recognising the model, but there's a strong chance that this is caused by my
        > incorrect schema definition.
        >
        > In the sample xsd (below) a scenario object can contain a ListA - which
        > contains multiple instances of a Model (or child thereof). It can a ListB
        > which includes instances of the same objects (defined using my "old" way of
        > doing it).
        >
        > My editor can see that into ListA I can but objects of type A or type B, but
        > it isn't aware of their properties = whereas when I insert them into ListB -
        > it provides direct access to their properties.
        >
        > Is this because my schema definition is too vague to provide it with that
        > information? Or is this in fact a problem with my XML editor?
        >
        > Many thanks in advance,
        >
        > Ian Mayo
        >
        >
        >
        > =============== =============== =============== =============== =
        >
        > <?xml version="1.0" encoding="UTF-8"?>
        > <!-- edited with XMLSPY v5 rel. 4 U (http://www.xmlspy.com) by Ian Mayo
        > (PlanetMayo Ltd) -->
        > <!--W3C Schema generated by XMLSPY v5 rel. 4 U (http://www.xmlspy.com)-->
        > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        > elementFormDefa ult="qualified" >
        >
        > <xs:element name="Scenario" >
        > <xs:annotatio n>
        > <xs:documentati on>Container for a chain</xs:documentatio n>
        > </xs:annotation>
        > <xs:complexType >
        > <xs:sequence>
        > <xs:element name="ListA" type="ListTypeA "/>
        > <xs:element name="ListB" type="ListTypeB "/>
        > </xs:sequence>
        > </xs:complexType>
        > </xs:element>
        >
        > <xs:complexTy pe name="ListTypeA ">
        > <xs:choice minOccurs="0" maxOccurs="unbo unded">
        > <xs:element ref="Model" />
        > </xs:choice>
        > </xs:complexType>
        >
        > <xs:complexTy pe name="ListTypeB ">
        > <xs:choice minOccurs="0" maxOccurs="unbo unded">
        > <xs:element name="A" type="TypeA"/>
        > <xs:element name="B" type="TypeB"/>
        > </xs:choice>
        > </xs:complexType>
        >
        > <!-- -->
        > <!-- DECISION MODELS -->
        > <!-- -->
        > <xs:element name="Model" abstract="true"/>
        > <xs:complexTy pe name="ModelType ">
        > <xs:annotatio n>
        > <xs:documentati on>Base type for models</xs:documentatio n>
        > </xs:annotation>
        > <xs:attribute name="Name" type="xs:string " use="required"/>
        > </xs:complexType>
        >
        > <xs:element name="A" type="TypeA" substitutionGro up="Model"/>
        > <xs:complexTy pe name="TypeA">
        > <xs:complexCont ent>
        > <xs:extension base="ModelType ">
        > <xs:attribute name="attA_A" type="xs:float" use="required"/>
        > <xs:attribute name="attA_B" type="xs:float" use="required"/>
        > </xs:extension>
        > </xs:complexConte nt>
        > </xs:complexType>
        >
        > <xs:element name="B" type="TypeB" substitutionGro up="Model"/>
        >
        > <xs:complexTy pe name="TypeB">
        > <xs:complexCont ent>
        > <xs:extension base="ModelType ">
        > <xs:attribute name="attB_A" type="xs:string " use="required"/>
        > <xs:attribute name="attB_B" type="xs:string " use="required"/>
        > </xs:extension>
        > </xs:complexConte nt>
        > </xs:complexType>
        > </xs:schema>
        >
        >[/color]

        Comment

        • Ian Mayo

          #5
          Re: XML Polymorphism through inheritance?

          Thanks for coming back to me on this Eric,

          but my challenge is how to avoid having to specify in the schema that I am
          allowing objects of type A or type B, I just want to specify that elements
          of type Model (or members of that substitution group) can be inserted into
          the list.

          This will prevent me having to remember to change the ListType definition
          when I add a new model type. In my sample, the ListB element works ok (and
          that has been the way I've always done it), I just want to move forward to
          specifying sub-elements using the ListB type definition.

          Cheers,
          Ian


          "Eric Sirois" <esiroisREMOVES PAMFILTER@ca.ib m.com> wrote in message
          news:bg8j1k$sa6 $1@hanover.toro lab.ibm.com...[color=blue]
          > Hello Ian,
          >
          > If you want to use the globally defined elements <A> and <B>, change the
          > following content model
          >
          > <xs:complexTy pe name="ListTypeB ">
          > <xs:choice minOccurs="0" maxOccurs="unbo unded">
          > <xs:element name="A" type="TypeA"/>
          > <xs:element name="B" type="TypeB"/>
          > </xs:choice>
          > </xs:complexType>
          >
          > to:
          >
          > <xs:complexTy pe name="ListTypeB ">
          > <xs:choice minOccurs="0" maxOccurs="unbo unded">
          > <xs:element ref="A" />
          > <xs:element ref="B" />
          > </xs:choice>
          > </xs:complexType>
          >
          > There is a difference bettwen the two content models. The top defines
          > and use local element <A> and <B>. They are not the same as the two
          > global element defintion. Think of it as defining a local variable in
          > a method with the same name as global variable in a class.
          >
          > Kind regards,
          > Eric
          >
          >
          > Ian Mayo wrote:[color=green]
          > > Thanks again for that advice Michael.
          > >
          > > It's worked fine for defining my structure to model "the real world".
          > >
          > > Unfortunately my XML editor (XMLSpy) doesn't appear to be property
          > > recognising the model, but there's a strong chance that this is caused[/color][/color]
          by my[color=blue][color=green]
          > > incorrect schema definition.
          > >
          > > In the sample xsd (below) a scenario object can contain a ListA - which
          > > contains multiple instances of a Model (or child thereof). It can a[/color][/color]
          ListB[color=blue][color=green]
          > > which includes instances of the same objects (defined using my "old" way[/color][/color]
          of[color=blue][color=green]
          > > doing it).
          > >
          > > My editor can see that into ListA I can but objects of type A or type B,[/color][/color]
          but[color=blue][color=green]
          > > it isn't aware of their properties = whereas when I insert them into[/color][/color]
          ListB -[color=blue][color=green]
          > > it provides direct access to their properties.
          > >
          > > Is this because my schema definition is too vague to provide it with[/color][/color]
          that[color=blue][color=green]
          > > information? Or is this in fact a problem with my XML editor?
          > >
          > > Many thanks in advance,
          > >
          > > Ian Mayo
          > >
          > >
          > >
          > > =============== =============== =============== =============== =
          > >
          > > <?xml version="1.0" encoding="UTF-8"?>
          > > <!-- edited with XMLSPY v5 rel. 4 U (http://www.xmlspy.com) by Ian Mayo
          > > (PlanetMayo Ltd) -->
          > > <!--W3C Schema generated by XMLSPY v5 rel. 4 U[/color][/color]
          (http://www.xmlspy.com)-->[color=blue][color=green]
          > > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
          > > elementFormDefa ult="qualified" >
          > >
          > > <xs:element name="Scenario" >
          > > <xs:annotatio n>
          > > <xs:documentati on>Container for a chain</xs:documentatio n>
          > > </xs:annotation>
          > > <xs:complexType >
          > > <xs:sequence>
          > > <xs:element name="ListA" type="ListTypeA "/>
          > > <xs:element name="ListB" type="ListTypeB "/>
          > > </xs:sequence>
          > > </xs:complexType>
          > > </xs:element>
          > >
          > > <xs:complexTy pe name="ListTypeA ">
          > > <xs:choice minOccurs="0" maxOccurs="unbo unded">
          > > <xs:element ref="Model" />
          > > </xs:choice>
          > > </xs:complexType>
          > >
          > > <xs:complexTy pe name="ListTypeB ">
          > > <xs:choice minOccurs="0" maxOccurs="unbo unded">
          > > <xs:element name="A" type="TypeA"/>
          > > <xs:element name="B" type="TypeB"/>
          > > </xs:choice>
          > > </xs:complexType>
          > >
          > > <!-- -->
          > > <!-- DECISION MODELS -->
          > > <!-- -->
          > > <xs:element name="Model" abstract="true"/>
          > > <xs:complexTy pe name="ModelType ">
          > > <xs:annotatio n>
          > > <xs:documentati on>Base type for models</xs:documentatio n>
          > > </xs:annotation>
          > > <xs:attribute name="Name" type="xs:string " use="required"/>
          > > </xs:complexType>
          > >
          > > <xs:element name="A" type="TypeA" substitutionGro up="Model"/>
          > > <xs:complexTy pe name="TypeA">
          > > <xs:complexCont ent>
          > > <xs:extension base="ModelType ">
          > > <xs:attribute name="attA_A" type="xs:float" use="required"/>
          > > <xs:attribute name="attA_B" type="xs:float" use="required"/>
          > > </xs:extension>
          > > </xs:complexConte nt>
          > > </xs:complexType>
          > >
          > > <xs:element name="B" type="TypeB" substitutionGro up="Model"/>
          > >
          > > <xs:complexTy pe name="TypeB">
          > > <xs:complexCont ent>
          > > <xs:extension base="ModelType ">
          > > <xs:attribute name="attB_A" type="xs:string " use="required"/>
          > > <xs:attribute name="attB_B" type="xs:string " use="required"/>
          > > </xs:extension>
          > > </xs:complexConte nt>
          > > </xs:complexType>
          > > </xs:schema>
          > >
          > >[/color]
          >[/color]


          Comment

          Working...