XML elementsΒΆ

XML elements

XML does not include pre-defined elements. The programmer is free to set up and name elements suitable for the application when creating the XML document. XML can therefore be used to create any type of document. HTML, by contrast, does have a set of pre-defined elements, but the programmer is restricted to these elements only.

Since there are no pre-defined elements, the XML definition consists of only a bare-bones syntax. The very flexibility provided by enabling programmers to create their own elements requires that this syntax be strictly defined. The custom nature of XML documents demands custom programs to handle and display the information contained in them. The strict XML syntax gives XML documents a predictable form, and makes these programs easier to write. For example (unlike HTML) every XML element must have both a start tag and an end tag (or a special empty-element tag). Also, any nested element must be completely contained within the element that encloses it.

Since elements in an XML document are non-standard and may be designed by the programmer, it is necessary to ensure that they can be correctly handled and displayed by a browser. In an HTML page, a browser knows, for example, that an H1 element is a top-level heading and will format and display it accordingly. This is possible because this element is part of the HTML standard. With XML some other method must be used.

There are three basic ways of informing a browser how to format and display XML elements:

  1. Style sheet linking

    With this technique, a style sheet is linked to the XML document. A style sheet is a separate file that contains instructions for formatting the individual XML elements.

    Two types of style sheet can be used:

    1

    A cascading style sheet (CSS). Cascading style sheets are also used for HTML pages.

    2

    An Extensible Stylesheet Language (XSL) style sheet. This is considerably more powerful than a CSS and is designed specifically for XML documents.

  2. Data binding

    This option requires an HTML page to be created and linked to the XML document. Then standard HTML elements in the HTML page (such as SPAN or TABLE elements) are bound to the XML elements. The HTML elements then automatically display the information from the XML elements to which they are bound.

  3. Scripting

    This technique also requires an HTML page to be created and linked to the XML document. The programmer must then write script code to access and display individual XML elements. The browser exposes the XML document as a Document Object Model (DOM). This provides a large set of objects, properties, and methods that the script code can use to access, manipulate, and display the XML elements. See XML parsers.


RELATED TOPICS

XML