Mon 28 Jan 2008
Modeling Container Item Relationship in XML Schema
Posted by admin under programming , rest , webserviceNo Comments
In simple REST-style webservice situation you sometimes have to model container-item relationships and provide an XML Schema as a means for interface description.
Here is an example how to design the associated XML Schema.
Example of an XML document (container):
<ns2:container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
xmlns:ns2='http://xml.example.org/schema/container'
xsi:schemaLocation='http://xml.example.org/schema/container container.xsd'>
<ns2:item>
<ns2:value>A</ns2:value>
</ns2:item>
<ns2:item>
<ns2:value>B</ns2:value>
</ns2:item>
</ns2:container>
In REST you often want to give access to an item without the container wrapper. One solution is to provide two schema files – one for item and one for container:
container.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
targetNamespace="http://xml.example.org/schema/container_item"
xmlns:tns="http://xml.example.org/schema/container_item"
elementFormDefault="qualified">
<xsd:include schemalocation="item.xsd"/>
<xsd:element name="container">
<xsd:complextype>
<xsd:sequence>
<xsd:element ref="tns:item" maxoccurs="unbounded"/>
</xsd:sequence>
</xsd:complextype>
</xsd:element>
</xsd:schema>
item.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
targetNamespace="http://xml.example.org/schema/container_item"
xmlns:tns="http://xml.example.org/schema/container_item"
elementFormDefault="qualified">
<xsd:element name="item" type="tns:itemType">
<xsd:complextype name="itemType">
<xsd:sequence>
<xsd:element name="value" type="xsd:string"/>
</xsd:sequence>
</xsd:complextype>
</xsd:element>
</xsd:schema>