在 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 文档中)定义了 tableSchema
的 bar
和 foo
。
编辑添加:这里,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/