O'Reilly logo

XML Hacks by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

View Documents as Tables Using Generic CSS or XSLT

While XML documents come in all shapes and sizes, a common pattern makes it very easy to present information stored in XML as a table.

XML’s structural flexibility is impressive, but many applications are well-served using only a tiny subset of its capabilities, often following a pattern of three levels of nested elements where only the child elements have content. The pattern looks roughly like this:

<table>
  <row>
    <cell-1>...value...</cell-1>
    <cell-2>...value...</cell-2>
    <cell-3>...value...</cell-3>
    <cell-4>...value...</cell-4>
  </row>
  <row>
    <cell-1>...value...</cell-1>
    <cell-2>...value...</cell-2>
    <cell-3>...value...</cell-3>
    <cell-4>...value...</cell-4>
  </row>
  ....
</table>

where table is any root element, row is any container element, and the cell elements are leaf elements containing data. (The names of the elements and whether or not they use namespaces don’t matter, provided that the cell elements stay in the same order throughout.) This pattern is common for XML vocabularies representing database tables or query results, as well as those representing data that was originally comma- or tab-separated. Looking beyond the browser for a moment, Excel 2003 [Hack #15] can work with this data easily, and Access 2003 [Hack #16] can import and export it with minimal effort.

Take, for example, the document contracts.xml , which represents two rows of such a table (shown in Example 3-7).

Example 3-7. contracts.xml

<?xml-stylesheet ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required