views:

890

answers:

3

I have one class which has field of type DataTable.

I want to write XSD for this class. My problem is the datable structure is not fixed. The columns are added dynamically. How to write XSD for such a class?

Once the XSD is defined, I want to validate the class serialized to XML against the XSD.

A: 

If you are validating the serialized xml against an XSD that means it has to conform to some structure but it can be very flexible. You need to learn how to create an XSD and the various attributes and parameters. Whatever structure you want start defining it, once you start the process it will be not that difficult. After ask specific problem question and SO community will help.

Basic XSD tutorial http://www.w3schools.com/Schema/ You can liquid xml studio to create XSD. Its free and GUI based http://www.liquid-technologies.com/

Bhushan
A: 

Generally speaking, if columns are dynamic, XSD will have to be dynamic too. One solution might be to implement complementing function to the one that creates table columns, function that dynamically creates xls statement for the adequate xls element (that representing class table). Ofcourse, the one must define static parts of xsd in advance, and merge that with dynamically created parts of xsd.

Wor working with XML I suggest you Stylus Studio.

majkinetor
+1  A: 

Sounds like you need a meta-format, in which the schema defines a way of defining the columns, instead of defining specific columns itself.

This kind of xml tends to be ugly and verbose (for example, consider xmlrpc and soap). It also means that the schema can't validate the actual columns, and only that they've been defined correctly.

The XML would look something like this:

<DataTable>
  <column name="..." value="..."/>
  <column name="..." value="..."/>
</DataTable>

The XSD would be something like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="DataTable">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="column">
          <xs:complexType>
            <xs:attribute name="name" type="xs:string"/>
            <xs:attribute name="value" type="xs:string"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Of course, if you need structured values (not just strings), then you'll need something a little more complex. If you need arbitrary objects, they can be represented as a map for each object, with values that can be maps in turn etc. The element schema definition needs to be recursive, so it can hold another instance of itself. This is basically what xmlrpc and soap do.

EDIT This doesn't fit with your "columns", but an example is:

<object name="contact">
  <object name="home">
    <object name="tel">
       <string name="area" value="910"/>
       <string name="num" value="1234 5678"/>
    </object>
  </object>
  <object name="work">
    <object name="tel">
       <string name="area" value="701"/>
       <string name="num" value="8888 8888"/>
    </object>
    <object name="fax">
       <string name="area" value="701"/>
       <string name="num" value="9999 9999"/>
    </object>
  </object>
</object>

Basic idea of the grammar:

V --> string | O      // a Value is a string or an Object
O --> (K V)*          // an Object is list of named values (Key-Value pairs)

Changed, so the root is always an object, and named:

O ==> (string K | O)* K

An XSD for this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="object">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">

        <xs:element name="string">
          <xs:complexType>
            <xs:attribute name="name" type="xs:string"/>
            <xs:attribute name="value" type="xs:string"/>
          </xs:complexType>
        </xs:element>

        <xs:element ref="object"/>

      </xs:choice>
      <xs:attribute name="name" type="xs:string"/>
    </xs:complexType>
  </xs:element>

</xs:schema>
13ren