XML 重用问题

标签 xml xsd reusability

在 XML 中,是否可以重用元素?

具体来说,我要解决的问题如下。我想定义一个包含元素 tableSchema 和元素 dataSource 的元素 table。我希望以一种table可以引用其他地方定义的tableSchema的方式来做到这一点。因此,我可以有多个根据相同的tableSchema定义的报告。

为了澄清,我希望能够执行以下操作:

<report name="Report1">
  <page>
    <table>
      <!--reference to tableSchema named "foo"-->
      <dataSource>fooData</dataSource>
    </table>
  </page>
  <page>
    <table> 
      <!--reference to tableSchema named "bar"-->
      <dataSource>barData</dataSource>
    </table>
  </page>
</report>

<report name="Report2">
  <page>
    <table>
      <!--reference to tableSchema named "foo" (same as above!)-->
      <dataSource>anotherFooData</dataSource>
    </table>
  </page>
</report>

并且在其他地方(可能在同一个 XML 文档中)定义了 tableSchemabarfoo

编辑添加:这里,tableSchema 并不是指另一个 Xml 架构。我的意思是表中字段的定义。例如,我希望能够执行以下操作:

<tableSchema name="bar">
    <field>
      <displayName>bar1</displayName>
      <sourceName>bar1Source</sourceName>
      <format>Currency</format>
    </field>
    <field>
      <displayName>bar2</displayName>
      <sourceName>bar2Source</sourceName>
      <format>Text</format>
    </field>
</tableSchema>

<tableSchema name="foo">
    <field>
      <displayName>foo1</displayName>
      <sourceName>foo1Source</sourceName>
      <format>Percent</format>
    </field>
</tableSchema>

然后,在上面的 Report1 定义了一个包含两个 table 的报表,其中一个根据 tableSchema foo 进行格式化,第二个根据 tableSchema bar 进行格式化,Report2 定义包含一个table的报告code> 根据 tableSchema foo 进行格式化,并且该架构与 Report1 中的架构相同。

最佳答案

您没有提供有关创建/解析此 XML 的内容的上下文,但假设您对此有控制权,那么您可以为此定义自己的约定,例如:

<tableSchema ref="foo">

然后解析器将查找 id 为“foo”的元素来检索 tableSchema 信息。

关于XML 重用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/830756/

相关文章:

mysql - 在查询中包含可重复使用的文本 block

android - 如何为 Android ecommerce_tracker.xml 创建用于跟踪器的 Google Analytics SDK v4

xml - 可以用多种可能的复杂类型重载 XSD 元素吗?

具有元素和属性的 XSD 扩展

xml - 使用 XML 1.1 架构在全局范围内断言元素 ID 唯一性

java - 是否有重用 BufferedWriter 的模式?

xml - Golang:将xml转换为gzip时出现问题

xml - 基于子级子值返回多个值来选择元素的 XPath?

xsd - xsi :schemaLocation? 有什么用

Qt QML : in-file definition of reusable objects