To include an external well-formed XML document, the approach is very similar to the
parsed method. The method attribute may be set to either
XML, RSS, or RDF to
explicitly set the method to XML parsing. Including a src attribute that ends in any of
extensions will implicitly invoke
this method as well.
The delimiter attribute in this context sets the name of the element (tag) within
which to look for element fields to parse. For example, most of the relevant fields in an RDF file are contained directly
within the <item> element; for this reason, item
is the default delimiter element. For each delimiting element found, the entire
<include> region will be looped through once.
Like the parsed method, the XML method uses the
generalized <field> tag to display the contents of a field value. In this context,
a field value refers to the character data within a named element (tag) inside the delimiting element. Field values will
be displayed in the order in which they appear in the XML file unless a name attribute is
set within the <field> tag, assigning the name of the element field to output. For
example, a name="title" attribute refers to the character data within
<title> and </title> in the source XML document.
As an example, suppose that you have an XML source document called
languages related to PostgreSQL, with the following structure:
<?xml version="1.0" encoding="utf-8"?>
<notes>Web-based content language.</notes>
<notes>PostgreSQL procedural language.</notes>
In this scheme, notice that each language is described within the <language>
element. To parse such an XML file in the same manner as the RDF example described earlier, set the
delimiter attribute of the <include> tag to
language and the src attribute to
languages.xml. This is demonstrated in Example 13-24.
Example 13-24. Including an XML file
<include src="languages.xml" delimiter="language" method="xml">
Language Name: <field name="name" /><br />
Language Notes: <field name="notes" /><br />
When processed, the output of Example 13-24 would look like this:
Language Name: C<br />
Language Notes: Built-in language.<br />
Language Name: LXP<br />
Language Notes: Web-based content language.<br />
Language Name: PL/pgSQL<br />
Language Notes: PostgreSQL procedural language.<br />
Example 13-25 demonstrates the display of a simple RDF XML document. This example differs from
Example 13-24 in that it addresses, specifically, an RDF document. As a result, the
delimiter attribute can be omitted, since the default value of
is appropriate for the RDF schema.
Example 13-25. Including an RDF file
<include src="/home/web/ports/headlines/slashdot.rdf" lastblock="5">
<table border="0" cellspacing="1"><tr>
<td bgcolor="#ffffff" width="100%">
<div class="content">- <field name="title"></div>
<td bgcolor="#e0e0e8" width="100%">
<field name="link" type="url" link="Read More..." target="_blank">
Notice also the use of the lastblock attribute in Example 13-25, which was also
described in the Section called Including Token-Delimited Files
" earlier in this chapter. Both the firstblock and lastblock
attributes can also be used with XML, RDF, and RSS files to limit and offset which blocks of data are displayed.
Remember that any XML document you attempt to include through LXP
be well-formed, or the
parser will fail. XML parse errors should appear in the Apache error log, prefixed with [lxp] XML Parse Error.