如何在XSD中对数据库表建模? [英] How to model database tables in XSD?
问题描述
在XSD中像这样为地址建模数据库表似乎很简单:
It seems simple to model a database table for, say, addresses, like this in an XSD:
<xsd:complexType name="address_Type">
<!-- the columns of the database table for addresses -->
<xsd:sequence>
<xsd:element name="street" type="xsd:string" />
<xsd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
然后,您可以使用此表格以及其他一些不相关的数据(例如用户名)来定义XML格式,如下所示:
Then you can define an XML format using this tabel plus some other unrelated data, say, a user name, like this:
<xsd:element name="user" type="user" />
<!-- =========================================== -->
<xsd:complexType name="user">
<xsd:sequence>
<!-- username stands for all non-address fields -->
<xsd:element name="username" type="xsd:string" />
<!-- wrapper element for the address fields -->
<xsd:element name="address" type="address_Type" />
</xsd:sequence>
</xsd:complexType>
和XML可能是这样的:
and the XML could be like this:
<?xml version="1.0" encoding="utf-8"?>
<user>
<username>Albert Einstein</username>
<!-- with wrapper element for address -->
<address>
<street>Main Street</street>
<city>Ghost Town</city>
<state>Up State</state>
</address>
</user>
此方法可以很容易地扩展到具有地址列表或地址记录与其他表中的记录组合的XML.一个XSD可以通过这种方式涵盖许多XML格式.
This method is easily extensible to XML's with a list of addresses, or with an address record combined with records from other tables. One XSD could cover lots of XML formats this way.
但是,想到的人呢?为什么我需要那个愚蠢的地址"?如果我仅包含一个地址对象的实例,则为wrapper元素,以到达此XML:
However, what about someone who thinks, why do I need that stupid "address" wrapper element if I only include 1 instance of an address object, to arrive at this XML:
<?xml version="1.0" encoding="utf-8"?>
<user>
<username>Albert Einstein</username>
<!-- no wrapper element for address -->
<street>Main Street</street>
<city>Ghost Town</city>
<state>Up State</state>
</user>
在我看来,现在我们不能使用与上面的XSD相同的complexType用于地址了.
To me it seems that now we cannot use the same complexType for address as in the XSD above.
在我的公司中,IT组织分散了,如果我大喊您应该添加包装元素",我可能会感到喉咙痛.还是头疼.因此,我有责任改进XSD以处理带有和不带有wrapper元素的XML.
In my company, the IT organization is dispersed, and if I start yelling "you should add the wrapper element", I might just get a sore throat. Or a headache. So it is up to me to improve my XSD to cope with XML's with and without the wrapper element.
我试图添加一个没有名称的xsd:element,但这不起作用:
I tried to add an xsd:element without name, but that does not work:
<xsd:element type="address_Type" />
我找不到类型属性为xsd:element以外的complexType的xsd:实体.
I could not find an xsd: entity with type attribute to point to the complexType other than xsd:element.
我的问题是是否还有一种方法可以对最后一个XML重新使用complexType?
My question is if there still is a method to re-use the complexType for the last XML?
推荐答案
如果希望从现有的没有包装元素的内容模型中构成新的内容模型,请考虑使用 xs:group
机制:>
If you wish to compose new content models from existing content models without wrapper elements, consider using the xs:group
mechanism:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="user" type="user" />
<xsd:complexType name="user">
<xsd:sequence>
<xsd:element name="username" type="xsd:string" />
<!--
...
Many more xsd:element or xsd:groups might appear here.
...
-->
<xsd:group ref="address"/>
</xsd:sequence>
</xsd:complexType>
<xsd:group name="address">
<xsd:sequence>
<xsd:element name="street" type="xsd:string" />
<xsd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string" />
</xsd:sequence>
</xsd:group>
</xsd:schema>
如果您希望选择允许包装器,则可以使用 xsd:choice
:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="user" type="user" />
<xsd:complexType name="user">
<xsd:sequence>
<xsd:element name="username" type="xsd:string" />
<xsd:choice>
<xsd:group ref="address"/>
<xsd:element name="address">
<xsd:complexType>
<xsd:group ref="address"/>
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
<xsd:group name="address">
<xsd:sequence>
<xsd:element name="street" type="xsd:string" />
<xsd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string" />
</xsd:sequence>
</xsd:group>
</xsd:schema>
这篇关于如何在XSD中对数据库表建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!