Search code examples
xmlxsdreusability

XML Reuse Question


In XML, is it possible to reuse elements?

Specifically, the problem that I am trying to solve is the following. I want to define an element table that contains an element tableSchema and an element dataSource. I want to do this in a way that a tables can refer to a tableSchema defined elsewhere. Thusly, I can have multiple reports defining tables according to the same tableSchema.

To clarify, I would like be able to do the following:

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

and

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

and have tableSchemas bar and foo defined elsewhere, possibly in the same XML document.

Edited to add: Here, by tableSchema, I do not mean another Xml schema. I mean a definition the fields in a table. For example, I would like to be able to do the following:

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

Then, in the above, Report1 defines a report that contains two tables, one formatted according to the tableSchema foo, and a second formatted according to the tableSchema bar, and Report2 defines a report that contains one table that is formatted according to the tableSchema foo and that schema is the same as in Report1.


Solution

  • You didn't provide context as to what would be creating/parsing this XML but assuming you have control over this then you could define your own convention for this eg:

    <tableSchema ref="foo">
    

    Where the parser would then look for a element with the id of "foo" to retrieve the tableSchema info.