• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

root element to wrap all the complexType elements

 
thomas silver
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could one make a root element to wrap all the complexType elements? I tried the followings and it did NOT work. Thank you
====
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<xs:element name="root"> <--- NOT working
<xs:complexType name="a">
....
</xs:complexType>
<xs:complexType name="b">
....
</xs:complexType>
<xs:complexType name="c">
....
</xs:complexType>
</xs:element> <--- NOT working
</xs:schema>
 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your sample code puzzles me. Are you looking to use Java to create a DOM document with a root element of "xs:schema" which in turn has a single child element having a local name "root", and then create child elements for "root" in the "xs" namespace having a local name of "complexType",

Or

are you trying to transform one XML document to another using XSLT giving these results?
 
vinay kumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your root element contains many other elements, first it needs to be defined as complextype. And define a,b and c as complextypes.
It could be written as following:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="a">
<xs:complexType>
....
</xs:complexType>
</xs:element>

<xs:element name="b">
<xs:complexType>
....
</xs:complexType>
</xs:element>

</xs:sequence>
</xs:complexType>
</xs:element>

And you can also define a and b complexTypes seperately and refer them in your root definition as below:
<xs:element name="aType">
<xs:complexType>
....
</xs:complexType>
</xs:element>

<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="aType"/>
</xs:sequence>
</xs:complexType>
</xs:element>

Hope this helps..
vnay.
[ September 26, 2006: Message edited by: vinay kumar ]
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinay,

And you can also define a and b complexTypes seperately and refer them in your root definition as below:




I dont think you can define "aType" as an element and then use it as a type. You should define it as follows :


Now use can use it with type="aType".


/Tom
 
thomas silver
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinay,

I tried to use your first way, but got error, perhaps because of inheritance(?). Please help.
======
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<xs:element name="a" abstract="true">
<xs:complexType>
<xs:sequence>
...
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="b">
<xs:complexType>
<xs:complexContent>
<xs:extension base="a">
<xs:sequence>
...</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
 
vinay kumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh.. you are right Tom. I didn't notice that. I was struggling to write that code in this forum's editor,with alignments.. So depended on copy paste. Mistake happened becuase of that.

Thanks for that..
vinay
 
vinay kumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As pointed out by Tom
Instead of

<xs:element name="a" abstract="true">
<xs:complexType>
<xs:sequence>
...
</xs:sequence>
</xs:complexType>
</xs:element>

Please use:
<xs:complexType name="a">
<xs:sequence>
...
</xs:sequence>
</xs:complexType>

In our xml instance: Root element is b. and it's content are elements defined in a type + the elements defined by it's own. Hope I am making sense.
 
thomas silver
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that doesn't work if I try to make a root element encompassing all those elements. For example, please make a root element out of the following:
===
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<xs:complexType name="a" abstract="true">
<xs:sequence>
...
</xs:sequence>
</xs:complexType>


<xs:complexType name="b">
<xs:complexContent>
<xs:extension base="a">
<xs:sequence>
...
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

</xs:schema>
[ September 27, 2006: Message edited by: thomas silver ]
 
vinay kumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok .. lets replace the dots(...) of your schema with some elements:

For this xml schema:


cheers,
Vinay
 
thomas silver
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinay,

Thanks for your patience. Is it possible to put a root element in the xsd file (instead in the xml file) in your example? I am using xmlspy to generate the xml file from the xsd file and it complains of no root element. What I have is more than 10 complexType elements, including those containing 'extension'. I still have problem putting a root element in the xsd... at this point, I am not so sure it's possible or not. Thanks much.
[ September 27, 2006: Message edited by: thomas silver ]
 
vinay kumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry.. I have given b element declaration wrong (Excuse me please, I dont have validation tool here): Take this



There is no such xs:root construct in xsd. To make an element 'X' as root of the document, simply include all your elements in 'X' declaration.

In the above xsd document 'b' is made as root element.

Thanks
vinay.
 
thomas silver
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinay,

But that didn't work. Please see my first post. Maybe I should give it up on this one. Thanks anway though.
[ September 29, 2006: Message edited by: thomas silver ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic