Re: [xml] Line numbers and XInclude

On Mon, Sep 13, 2010 at 12:07:01PM +1000, Michael Day wrote:

After performing XInclude processing is there any way of identifying
which nodes in the tree came from which file originally?

  Well, there is but it's not completely trivial.
First normally XInclude should now add a base attribute to the XIncluded
top elements (unless disabled) and that should be sufficient, for
example xmlNodeGetBase() should work in that case. Of course there
is always the possibility to XInclude non-elements and that defeats
  The other possibility is that libxml2 XInclude keeps the original
<xi:include> elements but turns them into XML_XINCLUDE_START nodes,
it also adds an XML_XINCLUDE_END element to mark the end of the
included block, so an algorithm like

  for all node n in the node ancestors
     for all nodes l in the n predecessors
         if l type is XML_XINCLUDE_END
            break (next ancestor)
         if l type is XML_XINCLUDE_START
            that's the xi:xinclude node for the node

you may have to recurse as XInclude can cascade, but basically you get
the full informations kept in the tree. Finding back the actual URI for
each can be a bit painful due to the recursion but that should be

The reason I ask is that it can be convenient to have file and line
number information for error messages, but it seems that XInclude
will result in a document containing nodes with line numbers that
are from a different file, leading to user confusion.

  xmlNodeGetBase() should get it right at least if the xml:base
  attributes are kept.

Is there any workaround for this other than implementing XInclude
resolution "by hand"?

  I think the 2 methods above provide an heuristic and the possibility
for an exact algorithm :-)


Daniel Veillard      | libxml Gnome XML XSLT toolkit
daniel veillard com  | Rpmfind RPM search engine | virtualization library

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