XSD:向强类型“简单"添加属性元素 [英] XSD: Adding attributes to strongly-typed "simple" elements

查看:17
本文介绍了XSD:向强类型“简单"添加属性元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一些明智的方法来让元素具有强类型的简单类型和属性?

好的,我有一个 XSD 架构,其中包含一百万个(呃,一百个)元素,可能看起来像这样:

<xsd:element name="DocumentDescription" type="xsd:string"/><xsd:element name="DocumentDateTime" type="xsd:dateTime"/><xsd:element name="DocumentSize" type="xsd:int"/>

那是花花公子.但是,我真的希望所有这些元素也有一些共同的属性,比如格式"和isVisible".即有这样的架构:

<DocumentDescription isVisible="true">文档描述</DocumentDescription><DocumentDateTime 格式="dd/mm/yyyy" isVisible="true">1/1/2008</DocumentDescription><DocumentSize 格式="0.00 KB" isVisible="false">5403</DocumentSize>

我可以手动完成,非常可怕的是,在生成 XSD 时将所有此类属性添加到 XSD,如下所示:

<xsd:element name="DocumentDescription"/><xsd:complexType><xsd:simpleContent><xsd:extension base="xsd:string"><xsd:属性名="格式" type="xsd:string"/><xsd:属性名称="isVisible" type="xsd:boolean"/></xsd:扩展名></xsd:simpleContent></xsd:complexType><xsd:元素名称="DocumentDateTime"/>... 等等

...但在理想世界中,我宁愿将其定义为 complexType:

<xsd:complexType name="customType"><xsd:complexContent><xsd:extension base="???"><xsd:属性名="格式" type="xsd:string"/><xsd:属性名称="isVisible" type="xsd:boolean"/>

...这意味着我可以这样做:

<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"/>

我的理想世界"代码的问题在于:

a) 我没有有效的 <xsd:extension base-"???">,因为我真的不在乎我在扩展什么;我想扩展所有类型.似乎xsd:anyType"是合适的,但是元素变成了弱类型容器不是吗?

b) 我不能再在 <xsd:element> 上指定简单类型,因为现在类型是我定义的复杂customType".因此,我放在那里的想象中的baseType"属性......

那么我可以以一种不笨拙的方式为简单类型添加属性吗?或者我是否需要定义十几个除了它们扩展的简单类型之外都相同的复杂类型?

强类型元素不仅可以更合理地描述数据,而且当我将它们用于 Excel 中的 XML 映射时(这就是这些东西背后的全部目的),强类型意味着 Excel 会正确设置单元格格式关于类型.

我可能看错了!任何建议表示赞赏.

解决方案

<块引用>

[quote] 可以手动完成,而且很可怕,通过将所有此类属性添加到 XSD当我生成它时,就像这个:[/quote]

恐怕这是您唯一的正确"、兼容 XSD 架构的方式.

XSD 有时会让作者感到费解 - 但它有助于保证安全:-)

马克

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屋!

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