XSD:将属性添加到强类型“简单”分子 [英] XSD: Adding attributes to strongly-typed "simple" elements
问题描述
是否有一些明智的方法来使用强类型的简单类型和属性?元素有一个XSD模式,它有一个百万,百)元素可能如下所示:
< xsd:element name =DocumentDescriptiontype =xsd:string />
< xsd:element name =DocumentDateTimetype =xsd:dateTime/>
< xsd:element name =DocumentSizetype =xsd:int/>
这是dandy。然而,我真的希望所有这些元素也有一些常见的属性,比如说,格式和是可视。即具有如下所示的模式:
< DocumentDescription isVisible =true> doc description< / DocumentDescription>
< DocumentDateTime format =dd / mm / yyyyisVisible =true> 1/1/2008< / DocumentDescription>
< DocumentSize format =0.00 KBisVisible =false> 5403< / DocumentSize>
我可以手动执行,可怕的是,当我生成它时,将所有这些属性添加到XSD ,如下所示:
< xsd:element name =DocumentDescription/>
< xsd:complexType>
< xsd:simpleContent>
< xsd:extension base =xsd:string>
< xsd:attribute name =formattype =xsd:string/>
< xsd:attribute name =isVisibletype =xsd:boolean/>
< / xsd:extension>
< / xsd:simpleContent>
< / xsd:complexType>
< xsd:element name =DocumentDateTime/>
... etc
...但在一个理想的世界,我宁愿定义它作为一个complexType:
< xsd:complexType name =customType>
< xsd:complexContent>
< xsd:extension base =???>
< xsd:attribute name =formattype =xsd:string/>
< xsd:attribute name =isVisibletype =xsd:boolean/>
...这意味着我可以做:
< xsd:element name =DocumentDescriptiontype =customTypebaseType =xsd:string/>
< xsd:element name =DocumentDateTimetype =customTypebaseType =xsd:dateTime/>
< xsd:element name =DocumentSizetype =customTypebaseType =xsd:int/>
我的理想世界代码的问题是:
a)我没有有效的< xsd:extension base - ???
>,不在乎我在延伸什么我想扩展所有类型。似乎像xsd:anyType是适当的,然后元素成为弱类型的容器吗?
b)我可以不再在< xsd:element
>上指定简单类型,因为现在该类型是我定义的复杂的customType。因此,我放在那里的虚构的baseType属性...
那么我可以以非笨重的方式为简单类型添加属性?或者我需要定义一个完全相同的complexTypes,除了它们扩展的简单类型?
强类型的元素不仅可以更明智地描述数据,而且当我使用它们在Excel中的XML映射(这是这些东西的全部目的)时,强类型意味着Excel会根据类型正确设置单元格格式。
我可能看着这一切都错了!任何建议赞赏。
[quote]可以手动,可怕的是,
将所有这些属性添加到XSD
当我生成它,像
这样:[/ quote]
我恐怕这是你唯一的正确的XSD模式兼容的方式。
$ b $可以将XSD复制到作者,但有助于保持安全: - )
Marc
Is there some sensible way to have elements with strongly-typed simple-types and also attributes?
Okay, I have an XSD schema which has a million (er, hundred) elements that might look like this:
<xsd:element name="DocumentDescription" type="xsd:string" />
<xsd:element name="DocumentDateTime" type="xsd:dateTime" />
<xsd:element name="DocumentSize" type="xsd:int" />
That's dandy. However, I really want all of these elements to also have some common attributes on them like, let's say, "format" and "isVisible". i.e. have a schema like:
<DocumentDescription isVisible="true">doc description</DocumentDescription>
<DocumentDateTime format="dd/mm/yyyy" isVisible="true">1/1/2008</DocumentDescription>
<DocumentSize format="0.00 KB" isVisible="false">5403</DocumentSize>
I could do it manually, and horribly, by adding all such attributes to the XSD when I generate it, something like this:
<xsd:element name="DocumentDescription" />
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="format" type="xsd:string" />
<xsd:attribute name="isVisible" type="xsd:boolean" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:element name="DocumentDateTime" />
... etc
...but in an ideal world I'd rather define it as a complexType:
<xsd:complexType name="customType">
<xsd:complexContent>
<xsd:extension base="???">
<xsd:attribute name="format" type="xsd:string" />
<xsd:attribute name="isVisible" type="xsd:boolean" />
...which means I could just do:
<xsd:element name="DocumentDescription" type="customType" baseType="xsd:string" />
<xsd:element name="DocumentDateTime" type="customType" baseType="xsd:dateTime" />
<xsd:element name="DocumentSize" type="customType" baseType="xsd:int" />
The problem with my "ideal world" code is that:
a) I've no valid <xsd:extension base-"???"
>, since really I don't care what I'm extending; I want to extend all types. Seems like the "xsd:anyType" is appropriate, yet then the element becomes a weakly typed container does it not?
b) I can no longer specify the simple type on the <xsd:element
>, since now the type is the complex "customType" I defined. Hence the imaginary "baseType" attribute I put there...
So can I add attributes to simple types in a non-clunky way? Or do I need to define a dozen complexTypes that are all identical except for the simple type that they extend?
Strongly-typed elements not only describe the data more sensibly, but when I use them for XML mapping in Excel (and this is the whole purpose behind these things), the strong-typing means that Excel sets the cell formatting correctly based on the type.
I'm probably looking at it all the wrong way! Any advice appreciated.
[quote]could do it manually, and horribly, by adding all such attributes to the XSD when I generate it, something like this:[/quote]
I'm afraid this is your only "proper", XSD-schema compatible way to do it.
XSD can be convoluted to author at times - but it helps keeping things safe :-)
Marc
这篇关于XSD:将属性添加到强类型“简单”分子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!