wood burning stoves 2.0*
The moose likes Product and Other Certifications and the fly likes xsd for complex element structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "xsd for complex element structure" Watch "xsd for complex element structure" New topic
Author

xsd for complex element structure

damian cosmos
Ranch Hand

Joined: Dec 13, 2001
Posts: 32
Hi all,
I have a piece of xml for which I am trying to create the xsd equivalent:
<stream onid='01' tsid='90' streamname='ASTRA1'>
<service sid='7f3'>bbc</service>
<service sid='722'>tg4</service>
<service sid='233'/>
</stream>
The onid and tsid attributes are required but the streamname attribute is optional.
The service tag can occur zero or an unbounded number of times.
The sid attribute is required.
I have come up with the following piece of xsd to define the schema for the xml above:
<xsd:element name="stream" minOccurs="0">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="service" minOccurs="0">
<xsd:complexType>
<xsd:attribute name="sid" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="onid" type="xsd:string" use="required"/>
<xsd:attribute name="tsid" type="xsd:string" use="required"/>
<xsd:attribute name="streamname" type="xsd:string"/>
</xsd:complexType>
</xsd:element>

The schema works for
<stream onid='01' tsid='90' streamname='ASTRA1'>
<service sid='7f3'></service>
<service sid='722'></service>
<service sid='233'/>
</stream>
but not for
<stream onid='01' tsid='90' streamname='ASTRA1'>
<service sid='7f3'>bbc</service>
<service sid='722'>tg4</service>
<service sid='233'/>
</stream>
Any help would be greatly appreciated.
Regards,
Damian.
Hari Vignesh Padmanaban
Ranch Hand

Joined: Oct 07, 2003
Posts: 578
Hi Damian,

Your schema will work only for element with attributes, and no textual content.
i.e

is for ELEMENTS WITH ATTRIBUTES BUT NO CONTENT !!!

A more complicated definition would be for an elemnt with textual content and an attribute. You would have to use the simpleContent tag inside the complex type to achive that !! So here is the code !!!

And few more suggestions :-)
The minOccurs should not be set to 0 as it woul mean that you are giving a default value of 1 for maxOccurs. A betetr thing would be to give "unbounded" as I have
Try to make a complex type and then make an elemnt as that type !!!. Even more better way would be to create an attribute group. The code is as follows :-)

Writting this way, makes your code more flexible, efficient, easy to read and BEST OF ALL extensible !!!
Hope this helps
[ February 17, 2004: Message edited by: Hari Vignesh Padmanaban ]
damian cosmos
Ranch Hand

Joined: Dec 13, 2001
Posts: 32
Hi Hari,
Thanks for the post.
I figured it out a different way as follows using
<xsd:complexType mixed="true">:
<xsd:element name="stream" minOccurs="0">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="service" minOccurs="0">
<xsd:complexType mixed="true">
<xsd:sequence/>
<xsd:attribute name="sid" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="onid" type="xsd:string" use="required"/>
<xsd:attribute name="tsid" type="xsd:string" use="required"/>
<xsd:attribute name="streamname" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
I reckon you're way is more correct.
Thanks for all your help.
Regards,
Damian.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xsd for complex element structure