包含DataTable的类的XML模式 [英] XML Schema for class containing a DataTable

查看:118
本文介绍了包含DataTable的类的XML模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类的类型为 DataTable 的类。

I have one class which has field of type DataTable.

我想为这个类写XSD。我的问题是datable结构是不固定的。这些列是动态添加的。如何写这样一个类的XSD?

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?

一旦定义了XSD,我想根据XSD验证序列化为XML的类。

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

推荐答案

听起来你需要一个元格式,其中模式定义了一种定义列的方法,而不是定义特定的列本身。

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

这种xml往往是丑陋和冗长的(例如,考虑xmlrpc和soap)。这也意味着模式无法验证实际的列,只有它们已被正确定义。

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.

XML将如下所示:

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

XSD将是这样的:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <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>

当然,如果你需要结构化的值(不只是字符串),那么你需要一些一点点复杂如果您需要任意对象,它们可以表示为每个对象的映射,其值可以依次映射。元素模式定义需要是递归的,因此它可以保存其自身的另一个实例。这基本上是xmlrpc和soap做的。

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.

编辑这不符合你的列,但一个例子是: p>

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>

语法的基本思想:

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

所以root总是一个对象,并命名为:

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

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

这是一个XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <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>

这篇关于包含DataTable的类的XML模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆