xml inheritance random order

Discussion in 'XML' started by wanda, May 9, 2006.

  1. wanda

    wanda Guest

    Hi,

    I am trying to use XML inheritance to define a base class and derived
    class that can have elements appear in any order. from what i've read,
    i can only extend the base class in sequential order. does anyone know
    how to do this? for example, if i want to allow both of these:

    <employee>
    <firstname>larry</firstname>
    <lastname>jones</lastname>
    </employee>

    <employee>
    <lastname>smith</lastname>
    <firstname>mary</firstname>
    <city>new york</city>
    </employee>

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">


    <xs:element name="employee" type="fullpersoninfo"/>
    <xs:complexType name="personinfo">
    <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
    </xs:complexType><xs:complexType name="fullpersoninfo">
    <xs:complexContent>
    <xs:extension base="personinfo">
    <xs:sequence>
    <xs:element name="address" type="xs:string"/>
    <xs:element name="city" type="xs:string"/>
    <xs:element name="country" type="xs:string"/>
    </xs:sequence>
    </xs:extension>
    </xs:complexContent>
    </xs:complexType>
    </xs:schema>
    wanda, May 9, 2006
    #1
    1. Advertising

  2. wanda

    George Bina Guest

    Hi,

    A schema that validates both your instances is below. It adds also the
    lastname, firstname sequence with a choice with the initial firstname,
    lastname sequence and makes the elements added in the extended type
    optional:

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="employee" type="fullpersoninfo"/>
    <xs:complexType name="personinfo">
    <xs:choice>
    <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
    <xs:sequence>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="firstname" type="xs:string"/>
    </xs:sequence>
    </xs:choice>
    </xs:complexType>
    <xs:complexType name="fullpersoninfo">
    <xs:complexContent>
    <xs:extension base="personinfo">
    <xs:sequence>
    <xs:element name="address" type="xs:string" minOccurs="0"/>
    <xs:element name="city" type="xs:string" minOccurs="0"/>
    <xs:element name="country" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    </xs:extension>
    </xs:complexContent>
    </xs:complexType>
    </xs:schema>

    Best Regards,
    George
    ---------------------------------------------------------------------
    George Cristian Bina
    <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
    http://www.oxygenxml.com
    George Bina, May 10, 2006
    #2
    1. Advertising

  3. wanda

    wanda Guest

    hi,

    that would work, however my base class has 100+ elements, which can be
    in any order and they are all optional. the derived class's elements
    are also all optional and can be in any order as well. i dont believe
    the above solution will work for my situation. any thoughts? thanks
    for your response!
    wanda, May 10, 2006
    #3
  4. Give up and declare the base element's content as "any"?

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
    Joseph Kesselman, May 10, 2006
    #4
  5. wanda

    wanda Guest

    hi,

    i tried that, but for some reason my xml keeps coming up as invalid
    unless i declare the objects as xs:sequence, and that wont work for me.

    thanks anyway.
    wanda, May 10, 2006
    #5
  6. wanda

    ceasaro

    Joined:
    Jun 20, 2007
    Messages:
    1
    A solution for random order element in an xml schema.

    The following schema snippet allows a random order of the elementA, elementB and elementC elements within the root element.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
               xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
       <xs:element name="root" type="randomOrder"/>
    
       <xs:complexType name="randomOrder">
          <xs:sequence minOccurs="0" maxOccurs="unbounded">
             <xs:choice>
                <xs:element name="elementA" type="xs:string"/>
                <xs:element name="elementB" type="xs:string"/>
                <xs:element name="elementC" type="xs:string"/>
             </xs:choice>
          </xs:sequence>
       </xs:complexType>
    </xs:schema>
    
    examples of valid xml snippets.
    ...
    Code:
    <root>
       <elementA/>
       <elementB/>
       <elementC/>
       <elementA/>
    </root>
    
    <root>
       <elementC/>
       <elementC/>
       <elementC/>
       <elementB/>
    </root>
    
    <root>
       <elementC/>
       <elementA/>
       <elementB/>
       <elementB/>
       <elementA/>
       <elementC/>
       <elementA/>
       <elementB/>
    </root>
    hope this helps.
    ceasaro, Jun 20, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    441
    mikeb
    Jun 24, 2004
  2. Maziar Aflatoun

    Random not really random...

    Maziar Aflatoun, Aug 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    26,655
    Maziar Aflatoun
    Aug 5, 2004
  3. Sven
    Replies:
    11
    Views:
    1,189
    Pavel Lepin
    Nov 5, 2007
  4. globalrev
    Replies:
    4
    Views:
    739
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,110
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page