Skip to content

Commit

Permalink
[svn r300] Add simple XInclude support.
Browse files Browse the repository at this point in the history
--HG--
branch : trunk
  • Loading branch information
faassen committed May 20, 2005
1 parent f97fa38 commit eb12023
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -10,6 +10,9 @@ Features added
* parameters (XPath expressions) can now be passed to XSLT using
keyword parameters.

* Simple XInclude support. Calling the xinclude() method on a tree
will process any XInclude statements in the document.

* Added convenience xslt() method on tree. This is less efficient
than the XSLT object, but makes it easier to write quick code.

Expand Down
17 changes: 17 additions & 0 deletions doc/api.txt
Expand Up @@ -191,6 +191,23 @@ method to do RelaxNG validation::
>>> doc2.relaxng(relaxng_doc)
0

xinclude
--------

Simple XInclude support exists. You can make xinclude statements in a
document be processed by calling the xinclude() method on a tree::

>>> data = StringIO('''\
... <doc xmlns:xi="http://www.w3.org/2001/XInclude">
... <foo/>
... <xi:include href="doc/test.xml" />
... </doc>''')

>>> tree = lxml.etree.parse(data)
>>> tree.xinclude()
>>> lxml.etree.tostring(tree.getroot())
'<doc xmlns:xi="http://www.w3.org/2001/XInclude">\n<foo/>\n<a xml:base="doc/test.xml"/>\n</doc>'

write_c14n on ElementTree
-------------------------

Expand Down
1 change: 1 addition & 0 deletions doc/test.xml
@@ -0,0 +1 @@
<a/>
18 changes: 18 additions & 0 deletions src/lxml/etree.pyx
Expand Up @@ -5,6 +5,7 @@ cimport xpath
cimport xslt
cimport relaxng
cimport xmlerror
cimport xinclude
cimport c14n
cimport cstd
import types
Expand Down Expand Up @@ -67,6 +68,9 @@ class RelaxNGParseError(RelaxNGError):
class RelaxNGValidateError(RelaxNGError):
pass

class XIncludeError(Error):
pass

class C14NError(Error):
pass

Expand Down Expand Up @@ -246,6 +250,20 @@ cdef class _ElementTree(_DocumentBase):
"""
schema = RelaxNG(relaxng)
return schema.validate(self)

def xinclude(self):
"""Process this document, including using XInclude.
"""
cdef int result
# XXX what happens memory-wise with the original XInclude nodes?
# they seem to be still accessible if a reference to them has
# been made previously, but I have no idea whether they get freed
# at all. The XInclude nodes appear to be still being in the same
# parent and same document, but they must not be connected to the
# tree..
result = xinclude.xmlXIncludeProcess(self._c_doc)
if result == -1:
raise XIncludeError, "XInclude processing failed"

def write_c14n(self, file):
"""C14N write of document. Always writes UTF-8.
Expand Down
11 changes: 11 additions & 0 deletions src/lxml/tests/test_etree.py
Expand Up @@ -1681,6 +1681,16 @@ def test_relaxng_shortcut(self):
''')
self.assert_(tree_valid.relaxng(schema))
self.assert_(not tree_invalid.relaxng(schema))

class ETreeXIncludeTestCase(HelperTestCase):
def test_xinclude(self):
tree = etree.parse(fileInTestDir('test_xinclude.xml'))
# process xincludes
tree.xinclude()
# check whether we find it replaced with included data
self.assertEquals(
'a',
tree.getroot()[1].tag)

class ETreeC14NTestCase(HelperTestCase):
def test_c14n(self):
Expand All @@ -1699,6 +1709,7 @@ def test_suite():
suite.addTests([unittest.makeSuite(ETreeOnlyTestCase)])
suite.addTests([unittest.makeSuite(ETreeXSLTTestCase)])
suite.addTests([unittest.makeSuite(ETreeRelaxNGTestCase)])
suite.addTests([unittest.makeSuite(ETreeXIncludeTestCase)])
suite.addTests([unittest.makeSuite(ETreeC14NTestCase)])
suite.addTests(
[doctest.DocFileSuite('../../../doc/api.txt')])
Expand Down
4 changes: 4 additions & 0 deletions src/lxml/tests/test_xinclude.xml
@@ -0,0 +1,4 @@
<doc xmlns:xi="http://www.w3.org/2001/XInclude">
<foo/>
<xi:include href="test.xml" />
</doc>
6 changes: 6 additions & 0 deletions src/lxml/xinclude.pxd
@@ -0,0 +1,6 @@
from tree cimport xmlDoc

cdef extern from "libxml/xinclude.h":

cdef int xmlXIncludeProcess(xmlDoc* doc)

0 comments on commit eb12023

Please sign in to comment.