XML technologies / XML / XML in a Nutshell / XInclude / Fallbacks

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

Documents referencing resources on other sites are subject to all the usual problems of the Web: documents are deleted or moved, servers crash, DNS records are not updated quickly enough, and much more. The examples so far fail completely if the resource addressed by an href attribute cannot be found. XInclude however, allows authors to specify fallback content when a document is missing. Each XInclude element may contain a single xi:fallback child element. If the remote document cannot be loaded, the content of the xi:fallback element replaces the xi:include element in place of the remote document’s content. For example:

<xi:include href="AlanTuring.xml">
  <xi:fallback>Hoppla! Alan Turing war nicht zu finden!</xi:fallback>
</xi:include>

There are no restrictions on what a xi:fallback element may contain. It can contain plain text, a child element, mixed content, or another xi:include element to resolve if the top-level one cannot be resolved. For example, this xi:include element tries to load the same document from three different sites:

<xi:include href="http://www.beispiel.us/data.xml">
  <xi:fallback>
    <xi:include href="http://www.beispiel.fr/data.xml">
      <xi:fallback>
        <xi:include href="http://www.beispiel.cn/data.xml">
          <xi:fallback>Konnte das Dokument weder in den USA, Frankreich noch in China finden.</xi:fallback>
        </xi:include>
      </xi:fallback>
    </xi:include>
  </xi:fallback>
</xi:include>

An xi:include element must not have more than one xi:fallback child and must not contain other xi:include child elements or other child elements from the XInclude namespace. Otherwise, all children of an xi:include element that are outside the XInclude namespace are ignored and do not appear in the resulting document after inclusion. The xi:fallback element is ignored if the resource specified by the href attribute of its xi:include parent element loads successfully. A xi:fallback element may appear only as a child of an xi:include element. Having any other parent element is a fatal error.

Fallbacks are processed only for resource errors, usually I/O errors while loading the remote resource. Other problems—for example, a document that is not well-formed when parse="xml" was specified, a text document to be included containing characters forbidden in XML, or a syntax error within the xi:include element—are fatal errors. When any of these occurs, the processor simply stops processing and reports an error.

  

 

 

 

back to top