XML Schma - Addiing optional fields

This should cover W3C XML Schema, Relax NG and DTD related problems.
sgotur
Posts: 1
Joined: Fri Jul 08, 2005 6:01 am
Location: Ohio
Contact:

XML Schma - Addiing optional fields

Post by sgotur »

I have the following XML

<request>
<system>ABC</system>
<id>XYZ123</id>
<dynamic>
<serial>
<id>X123456</id>
<id>A123456</id>
</serial>
<pool>
<id>y123456</id>
<id>d123456</id>
</pool>
</dynamic>

</request>


Now I need to conver the xml to XSD with the following business rule

<dynamic> element is optional field

if the <dynamic> field is defined it should have either one of these fields or both <serial> & <pool>

if <serial> or <pool> is defined it should have atleast one element <id>

I'm not sure how to convert the above xml to XSD and I tried couple of conversion tool and they do not have the options...
any help is appreciated.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

Hello,

You can use the Convert to action from the toolbar to convert your XML instance to XML Schema and then edit the result and add the restrictions that you want:

- The reference to dynamic has the attribute minOccurs="0"

- The definition of dynamic contains a choice element that means serial or pool or serial and pool

- The definitions of serial and pool contain a reference to attribute id with the attribute maxOccurs="unbounded"

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="request">
<xs:complexType>
<xs:sequence>
<xs:element ref="system"/>
<xs:element ref="id"/>
<xs:element ref="dynamic" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="system" type="xs:NCName"/>
<xs:element name="dynamic">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:sequence>
<xs:element ref="serial"/>
<xs:element ref="pool" minOccurs="0"/>
</xs:sequence>
<xs:element ref="pool"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="serial">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="id"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="pool">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="id"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="id" type="xs:NCName"/>
</xs:schema>
Best regards,
Sorin
Post Reply