Re: LibXML2 Indentation

On Tue, Apr 29, 2003 at 01:29:23AM +0200, Ali Akcaagac wrote:
> Short question, is there an environment flag or something that allows
> LibXML2 to indent it's created files instead putting everything into one
> line ? e.g. force every GNOME application to write it's xml based
> configuration in a nicely indented way ?
> In the LibXML2 FAQ I was able to read something about $XMLINT_INDENT
> environment variable but there aren't any further informations about
> this and I'm not sure if this is what I want.

I recently worked around this for a program I'm working on that also
uses libxml.
As Daniel pointed out, whitespace is significant in XML.  However, when
you're using XML to represent structure in otherwise markup-free data,
there is a simple heuristic:  whitespace in nodes that contain any
non-text subnodes is never significant.  In that case, it would be safe
to let libxml to add its own blanks. 

There's a trick needed when loading the file again, though.
I quote from my own ChangeLog:
This is sorta interesting: we tell libxml to save the XML tree we create
with formatting (indentation and newlines). But then, once we load it
from disk, the tree now has that whitespace as extra text nodes
interspersed within the tree. When libxml writes out this new tree, it
sees these text nodes and assumes that whitespace is significant, which
means if we've edited the tree before writing it, it won't be
reformatted properly.

So we strip all those whitespace nodes from the tree when we load it.

(The (trivial) code that accomplishes that is here:

Anyway, adding something like that to gconf doesn't seem like it would
be too hard.

It would be really nice if there was some way to flag an xmlNodePtr to
say "whitespace is not significant within this node".  I'm afraid of
the possibility that doing so some horrible violation of some tenet of
libxml, though, so I haven't persued making a patch for libxml.  :)

      Evan Martin
martine cs washington edu

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]