Transforming XML to HTML
We often want to display XML within an HTML page, or render an XML
document as HTML for improved readability.
The following approaches are not strictly comparable since authors have dealt with
different special cases, for example rendering empty elements and handling processing instructions, comments and
namespaces. Progam size comparisions are not helpful here because the stylesheets are fetched from the database.
All transformations are appled to the same small XML document
stored in the eXist database. The timings are perhaps overpowered by set-up costs and a better comparison is afforded by applying each approach to a largish XML document - see
which uses a large XML file sampled from the IATI XML standard for international AID reporting.
The conclusion is that XSLT beats even simple recursive XQuery on this task.
The eXist XSLT handles more cases than the simple recursive XQuery but is still between 4 and 5
times faster on the example XML file.
We cannot simply include it in the HTML because the browser will then attempt
to parse it as HTML. At most you will see the text rendered and at worst strange things happen
if the tags happen to be HTML tags (like 'title')
If we want merely to show the XML indented but without syntax highlighting, we can serialise it with the eXist util:serialize() function within a <pre> tag
Syntax highlighting the XML or output with an alternative format requires a custom transformation, using either XQuery or XSLT to
define the transformation.
This approach uses a typeswitch transformation as described by Dan McCreary in
is defined by a separate CSS stylesheet
This approach uses a XSLT used in the eXist-db distribution XSL
. Here the color coding is hardcoded in
This transformation is inspired by the eXist stylesheet and uses the same
approach using dl and dd tags for the hierarchical structure but with simple recursion.
Rendering is roughly comparable, including the handing of empty elements. Color-coding is hard-coded.
Sometimes a grid view is more appropriate. This approach uses a recursive
transformation to a set of nested tables