xml inheritance random order

W

wanda

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>
 
G

George Bina

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
 
W

wanda

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!
 
W

wanda

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.
 
Joined
Jun 20, 2007
Messages
1
Reaction score
0
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top