tags:

views:

99

answers:

3

I am having a problem building a schema for this XML:

<record>
    <field index="1">data</field>
    <field index="2">data</field>
    <field index="3">data</field>
    <field index="4">data</field>
    <field index="5">data</field>
    <field index="6">data</field>
    <group index="7">
        <member index="1">data</member>
        <member index="2">data</member>
    </group>
    <field index="8">data</field>
    <field index="9">data</field>
    <group index="10">
        <member index="1">data</member>
    </group>
    <field index="11">data</field>
    <field index="12">data</field>
    <field index="13">data</field>
    <field index="14">data</field>
    <field index="15">data</field>
    <field index="16">data</field>
    <field index="17">data</field>
</record>


The problem is that, I don't know to build a schema for the "field" elements, I have many elements with the same name "field" but with different attributes "index".

Also I am having a problem building a schema for the multi values inside the the tag group.

A: 

Welcome, for the field, you could define something along these lines:

<xsd:element name="field" type="FieldType"/>
<xsd:complexType name="FieldType>
  <xsd:simpleContent>
    <xsd:extension base="xsd:string">
      <xsd:attribute name="index" type="xsd:integer" />
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>

As for the group, you have to use a sequence:

<xsd:element name="group" type="GroupType" />
<xsd:complexType name="GroupType">
  <xsd:sequence>
    <xsd:element name="member" type="MemberType" minOccurs="1" maxOccurs="unbounded" />
  </xsd:sequence>
  <xsd:attribute name="index" type="xsd:integer" />
</xsd:complexType>

I assumed you must have at least one member for each group. Also, you should define the MemberType. I omitted it because it's almost identical to the FieldType above mentioned.

JG
The OP probably also wants to validate that indexes are not repeated. It looks like this can be done with xsd:unique, which I've never used (in fact, it looks like xsd:unique can do context-dependent validation, so its time that I started working with it). See http://www.w3.org/TR/xmlschema-0/#specifyingUniqueness
kdgregory
A: 

Hi thanks for the help.

I I tried this. However, the problem is that field with attribute index = "1" means name , and field index = "2" for instance means DOB field index = "3" means email .. etc

which means I have to define all the field elements for all the attributes in the record, which I still don't know how to do it .. because I have multiple elements with the same name "field" but however, different values for attribute "index".

thanks again.

mmspeed
Then you need to change your XML to <record><name>data</name><DOB>data</DOB><email>data</email>...</record>
Kirk Broadhurst
A: 

You've just found out why this is a bad design for a schema. There's no way to associate a type with an index. You should have individual element names, with individual types. For instance:

<person xsi:noNamespaceSchemaLocation="Person.xsd" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    <name>
        <first>John</first>
        <last>Saunders</last>
    </name>
    <dob>1967-08-13</dob>
    <email>[email protected]</email>
</person>

Could come from the following schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="person">
        <xs:annotation>
            <xs:documentation>A person</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="first" type="xs:string"/>
                            <xs:element name="last" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="dob" type="xs:date"/>
                <xs:element name="email">
                    <xs:simpleType>
                        <xs:restriction base="xs:token"/>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

This sort of thing can restrict particular elements to particular data types, and more. The sort of thing you have cannot restrict the type of particular elements because they all need to support any data type.

John Saunders
I didn't create this bad design, it's shipped in with the system at my office, and I am suppose to create a schema for a web service.
mmspeed
Well, you can't restrict the types of the data. Make sure your managers know that the data format chosen by the authors of this XML will almost guarantee bad data being entered into the system. Maybe that will eventually get back to the right people, who will design a schema that actually matches the business requirements.
John Saunders