XSD:向强类型"简单"元素添加属性

Gav*_*vin 15 xml excel xsd

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

好的,我有一个XSD架构,它有一百万(呃,一百)个元素,可能如下所示:

<xsd:element name="DocumentDescription" type="xsd:string" />
<xsd:element name="DocumentDateTime" type="xsd:dateTime" />
<xsd:element name="DocumentSize" type="xsd:int" />
Run Code Online (Sandbox Code Playgroud)

那是花花公子.但是,我真的希望所有这些元素在它们上面也有一些共同的属性,例如,"format"和"isVisible".即具有如下架构:

<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>
Run Code Online (Sandbox Code Playgroud)

我可以通过在生成它时将所有这些属性添加到XSD来手动完成,并且非常可怕,如下所示:

<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
Run Code Online (Sandbox Code Playgroud)

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

<xsd:complexType name="customType">
  <xsd:complexContent>
    <xsd:extension base="???">
      <xsd:attribute name="format" type="xsd:string" />
      <xsd:attribute name="isVisible" type="xsd:boolean" />
Run Code Online (Sandbox Code Playgroud)

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

<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" />
Run Code Online (Sandbox Code Playgroud)

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

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

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

那么我可以用非笨重的方式向简单类型添加属性吗?或者我是否需要定义十几个复杂类型,除了它们扩展的简单类型外,它们都是相同的?

强类型元素不仅更合理地描述数据,而且当我在Excel中使用它们进行XML映射时(这是这些事情背后的全部目的),强类型意味着Excel根据类型正确设置单元格格式.

我可能正在以错误的方式看待它!任何建议表示赞赏

mar*_*c_s 3

[quote]可以手动完成,而且可怕的是,在生成 XSD 时将所有此类属性添加到 XSD,如下所示:[/quote]

恐怕这是您唯一“正确的”、XSD 模式兼容的方法。

XSD 有时可能会让作者感到困惑 - 但它有助于保证安全:-)

马克