Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[svn r300] Add simple XInclude support.

--HG--
branch : trunk
  • Loading branch information...
commit eb1202363071ae2b8fb054504fdaecb943e29950 1 parent f97fa38
faassen authored
View
3  CHANGES.txt
@@ -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.
View
17 doc/api.txt
@@ -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
-------------------------
View
1  doc/test.xml
@@ -0,0 +1 @@
+<a/>
View
18 src/lxml/etree.pyx
@@ -5,6 +5,7 @@ cimport xpath
cimport xslt
cimport relaxng
cimport xmlerror
+cimport xinclude
cimport c14n
cimport cstd
import types
@@ -67,6 +68,9 @@ class RelaxNGParseError(RelaxNGError):
class RelaxNGValidateError(RelaxNGError):
pass
+class XIncludeError(Error):
+ pass
+
class C14NError(Error):
pass
@@ -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.
View
11 src/lxml/tests/test_etree.py
@@ -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):
@@ -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')])
View
4 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>
View
6 src/lxml/xinclude.pxd
@@ -0,0 +1,6 @@
+from tree cimport xmlDoc
+
+cdef extern from "libxml/xinclude.h":
+
+ cdef int xmlXIncludeProcess(xmlDoc* doc)
+
Please sign in to comment.
Something went wrong with that request. Please try again.