2. Document Structure

2.1 The X2D entity

Each X2D entity is contained within an <x2d> tag. An X2D entity can exist as a standalone graphics file or as a fragment that can be embedded within a parent document (such as an XHTML page).

Within an X2D entity are vector graphic elements such as shapes, text, and images or groups of these. The following example shows an X2D entity containing four rectangles.

<x2d width="4" height="3">
  <description>Four separate rectangles</description>
  <rectangle width="20" height="60"/>
  <rectangle width="30" height="70"/>
  <rectangle width="40" height="80"/>
  <rectangle width="50" height="90"/>
 </x2d>

2.2 The file header and body: the <head> and <body> tags

Like HTML, X2D files can have a <head> and <body> section. The purpose of these tags is to separate static preprocessing elements from the actual dynamic content. For example, items such as the background or the intended display are defined within the <head> tag. The <body> tag contains the actual X2D content.

<x2d width="4" height="3">
  <head>
    <display type="WORKSTATION" dim="900 300"/>
    <background image="window.jpg"/>
  </head>
  <body>
    <rect id="rect1" width="20" height="60"/>
    <rect id="rect2" width="30" height="70"/>
    <rect id="rect3" width="40" height="80"/>
    <rect id="rect4" width="50" height="90"/>
  </body>
</x2d>

The <head> and <body> tags are optional. If they are not present (it is assumed that the <head> tag is the first tag to appear after an <x2d> tag), then the contents of the <x2d> tag compose the body and there is no header.

2.3 Declaring elements: the <declare> tag

Elements can be defined through the <declare> tag. When an element is declared, an instance of it is stored in a template dictionary. This instance can be "used" later to instantiate a new object.

When an element is declared, it is not drawn, only defined. It can later be referenced by its "id" attribute. If an element does not have an id attribute, then that element cannot be inserted into the template dictionary; so essentially that item was never declared.

<x2d width="4in" height="3in">
  <head/>
  <body>
    <declare>
      <rectangle id="rect1" width="20" height="60"/>
      <rectangle id="rect2" width="30" height="70"/>
      <rectangle id="rect3" width="40" height="80"/>
      <rectangle id="rect4" width="50" height="90"/>
    </declare>
  </body>
</x2d>

Objects that have an id attribute yet fall outside the scope of the <declare> tags will also be templated. In this method, objects are also rendered. In the following example, "rect1" is both rendered and made into a template object.

<x2d width="4in" height="3in">
  <body>
    <rect id="rect1" width="20" height="60"/>
  </body>
</x2d>

One exception to this rule applies to objects instantiated by the <use> tag (see next section). These objects will never be declared into the template dictionary. Only objects that are explicitly declared or created will be stored in the template dictionary.

Furthermore, the template dictionary will contain templates of objects as they were initially created. Suppose we had the following situation:

<x2d width="4in" height="3in">
  <body>
    <rect id="rect1" width="20" height="60"/>
    <!-- Some code that changes rect1's width to 40 -->
    <use ref="rect1" id="rect2"/>
  </body>
</x2d>

In this example rect2, which is instantiated from rect1 using the <use> tag, will still have the originally declared width of 20, not 40. Also note that rect2 is not templated.

2.4 The <use> tag

Elements that have been templated can be re-used (i.e. instanced) anywhere in the X2D document. The <use> element creates a new instance of a templated object found within the template dictionary. The new instance will be placed in the active document.

Whenever an object is instanced, its attributes can be instantiated as well to override the previously declared attributes. In the following example, a rectangle is declared to have width equal to 20. But when it is instanced as rect2, its width is 40.

<x2d width="4in" height="3in">
  <declare>
    <rectangle id="rect1" width="20" height="60"/>
  </declare>
  <use ref="rect1" id="rect2" width="40"/>
</x2d>

The <use> tag can only be used within the X2D body.