Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cf0ba8

Browse files
miss-islingtonbansalanjali2512
authored andcommitted
bpo-33187: Document ElementInclude (XInclude) support in ElementTree (GH-8861) (GH-15958)
(cherry picked from commit 97b817e) Co-authored-by: Anjali Bansal <[email protected]>
1 parent 8ee8ad2 commit 6cf0ba8

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

Doc/library/xml.etree.elementtree.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,95 @@ Functions
726726
:class:`Element` instance and a dictionary.
727727

728728

729+
.. _elementtree-xinclude:
730+
731+
XInclude support
732+
----------------
733+
734+
This module provides limited support for
735+
`XInclude directives <https://www.w3.org/TR/xinclude/>`_, via the :mod:`xml.etree.ElementInclude` helper module. This module can be used to insert subtrees and text strings into element trees, based on information in the tree.
736+
737+
Example
738+
^^^^^^^
739+
740+
Here's an example that demonstrates use of the XInclude module. To include an XML document in the current document, use the ``{http://www.w3.org/2001/XInclude}include`` element and set the **parse** attribute to ``"xml"``, and use the **href** attribute to specify the document to include.
741+
742+
.. code-block:: xml
743+
744+
<?xml version="1.0"?>
745+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
746+
<xi:include href="source.xml" parse="xml" />
747+
</document>
748+
749+
By default, the **href** attribute is treated as a file name. You can use custom loaders to override this behaviour. Also note that the standard helper does not support XPointer syntax.
750+
751+
To process this file, load it as usual, and pass the root element to the :mod:`xml.etree.ElementTree` module:
752+
753+
.. code-block:: python
754+
755+
from xml.etree import ElementTree, ElementInclude
756+
757+
tree = ElementTree.parse("document.xml")
758+
root = tree.getroot()
759+
760+
ElementInclude.include(root)
761+
762+
The ElementInclude module replaces the ``{http://www.w3.org/2001/XInclude}include`` element with the root element from the **source.xml** document. The result might look something like this:
763+
764+
.. code-block:: xml
765+
766+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
767+
<para>This is a paragraph.</para>
768+
</document>
769+
770+
If the **parse** attribute is omitted, it defaults to "xml". The href attribute is required.
771+
772+
To include a text document, use the ``{http://www.w3.org/2001/XInclude}include`` element, and set the **parse** attribute to "text":
773+
774+
.. code-block:: xml
775+
776+
<?xml version="1.0"?>
777+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
778+
Copyright (c) <xi:include href="year.txt" parse="text" />.
779+
</document>
780+
781+
The result might look something like:
782+
783+
.. code-block:: xml
784+
785+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
786+
Copyright (c) 2003.
787+
</document>
788+
789+
Reference
790+
---------
791+
792+
.. _elementinclude-functions:
793+
794+
Functions
795+
^^^^^^^^^
796+
797+
.. function:: xml.etree.ElementInclude.default_loader( href, parse, encoding=None)
798+
799+
Default loader. This default loader reads an included resource from disk. *href* is a URL.
800+
*parse* is for parse mode either "xml" or "text". *encoding*
801+
is an optional text encoding. If not given, encoding is ``utf-8``. Returns the
802+
expanded resource. If the parse mode is ``"xml"``, this is an ElementTree
803+
instance. If the parse mode is "text", this is a Unicode string. If the
804+
loader fails, it can return None or raise an exception.
805+
806+
807+
.. function:: xml.etree.ElementInclude.include( elem, loader=None)
808+
809+
This function expands XInclude directives. *elem* is the root element. *loader* is
810+
an optional resource loader. If omitted, it defaults to :func:`default_loader`.
811+
If given, it should be a callable that implements the same interface as
812+
:func:`default_loader`. Returns the expanded resource. If the parse mode is
813+
``"xml"``, this is an ElementTree instance. If the parse mode is "text",
814+
this is a Unicode string. If the loader fails, it can return None or
815+
raise an exception.
816+
817+
729818
.. _elementtree-element-objects:
730819

731820
Element Objects

Doc/tools/susp-ignored.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ library/xml.etree.elementtree,,:character,<fictional:character>Commander Clement
331331
library/xml.etree.elementtree,,:actor,"for actor in root.findall('real_person:actor', ns):"
332332
library/xml.etree.elementtree,,:name,"name = actor.find('real_person:name', ns)"
333333
library/xml.etree.elementtree,,:character,"for char in actor.findall('role:character', ns):"
334+
library/xml.etree.elementtree,,:xi,<document xmlns:xi="http://www.w3.org/2001/XInclude">
335+
library/xml.etree.elementtree,,:include, <xi:include href="source.xml" parse="xml" />
336+
library/xml.etree.elementtree,,:include, Copyright (c) <xi:include href="year.txt" parse="text" />.
334337
library/zipapp,,:main,"$ python -m zipapp myapp -m ""myapp:main"""
335338
library/zipapp,,:fn,"pkg.mod:fn"
336339
library/zipapp,,:callable,"pkg.module:callable"

0 commit comments

Comments
 (0)