xml - XSD:向强类型 "simple"元素添加属性

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

那是花花公子。然而,我真的希望所有这些元素也有一些共同的属性,比如“格式”和“可见”。即有一个像这样的模式:

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

我可以通过在生成 XSD 时将所有此类属性添加到 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

...但在理想情况下,我宁愿将其定义为复杂类型:

<xsd:complexType name="customType">
  <xsd:complexContent>
    <xsd:extension base="???">
      <xsd:attribute name="format" type="xsd:string" />
      <xsd:attribute name="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 正确设置单元格格式基于在类型上。

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

最佳答案

目前还不完全清楚您认为手动解决方案的哪个方面很糟糕;如果只是因为它们需要扩展 n 个不同的基类型而必须定义 n 个不同的类型,那么你就陷入困境了。

如果是必须为 formatisVisible 属性进行 n 不同声明的想法,那么您可能会发现它不那么可怕使用命名属性组来保存这些定义:

<xs:attributeGroup name="globals">
  <xs:attribute name="format" type="xs:string"/>
  <xs:attribute name="isVisible" type="xs:boolean"/>
</xs:attributeGroup>

您需要的各种复杂类型的声明仍然是重复的,但现在稍微不那么冗长了:

<xs:complexType name="string">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attributeGroup ref="my:globals"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
<xs:complexType name="dateTime">
  <xs:simpleContent>
    <xs:extension base="xs:dateTime">
      <xs:attributeGroup ref="my:globals"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
<xs:complexType name="int">
  <xs:simpleContent>
    <xs:extension base="xs:int">
      <xs:attributeGroup ref="my:globals"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

现在你的元素声明比你的“理想”情况稍微简单一些:

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

关于xml - XSD:向强类型 "simple"元素添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/653158/

相关文章:

xml - 如何覆盖父/扩展元素内的 Xsd 元素

xsd - targetNamespace 和 xmlns :target? 之间有什么区别

android - 如何删除三个按钮之间的空格

android - 垂直对齐重力没有响应

xml - XPathException 无效 token

excel - VLOOKUP 未正确匹配

c# - Restsharp XML 反序列化

Excel VBA : inserting formula into a cell gives Run-time error '1004'

c# - 在列上添加特定的自动过滤器

xml - 使用 Schematron 和 xsltproc 验证 XSD 架构