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

Skip to content

Commit 97b817e

Browse files
bansalanjali2512scoder
authored andcommitted
bpo-33187: Document ElementInclude (XInclude) support in ElementTree (GH-8861)
1 parent 1660a61 commit 97b817e

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
@@ -738,6 +738,95 @@ Functions
738738
:class:`Element` instance and a dictionary.
739739

740740

741+
.. _elementtree-xinclude:
742+
743+
XInclude support
744+
----------------
745+
746+
This module provides limited support for
747+
`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.
748+
749+
Example
750+
^^^^^^^
751+
752+
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.
753+
754+
.. code-block:: xml
755+
756+
<?xml version="1.0"?>
757+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
758+
<xi:include href="source.xml" parse="xml" />
759+
</document>
760+
761+
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.
762+
763+
To process this file, load it as usual, and pass the root element to the :mod:`xml.etree.ElementTree` module:
764+
765+
.. code-block:: python
766+
767+
from xml.etree import ElementTree, ElementInclude
768+
769+
tree = ElementTree.parse("document.xml")
770+
root = tree.getroot()
771+
772+
ElementInclude.include(root)
773+
774+
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:
775+
776+
.. code-block:: xml
777+
778+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
779+
<para>This is a paragraph.</para>
780+
</document>
781+
782+
If the **parse** attribute is omitted, it defaults to "xml". The href attribute is required.
783+
784+
To include a text document, use the ``{http://www.w3.org/2001/XInclude}include`` element, and set the **parse** attribute to "text":
785+
786+
.. code-block:: xml
787+
788+
<?xml version="1.0"?>
789+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
790+
Copyright (c) <xi:include href="year.txt" parse="text" />.
791+
</document>
792+
793+
The result might look something like:
794+
795+
.. code-block:: xml
796+
797+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
798+
Copyright (c) 2003.
799+
</document>
800+
801+
Reference
802+
---------
803+
804+
.. _elementinclude-functions:
805+
806+
Functions
807+
^^^^^^^^^
808+
809+
.. function:: xml.etree.ElementInclude.default_loader( href, parse, encoding=None)
810+
811+
Default loader. This default loader reads an included resource from disk. *href* is a URL.
812+
*parse* is for parse mode either "xml" or "text". *encoding*
813+
is an optional text encoding. If not given, encoding is ``utf-8``. Returns the
814+
expanded resource. If the parse mode is ``"xml"``, this is an ElementTree
815+
instance. If the parse mode is "text", this is a Unicode string. If the
816+
loader fails, it can return None or raise an exception.
817+
818+
819+
.. function:: xml.etree.ElementInclude.include( elem, loader=None)
820+
821+
This function expands XInclude directives. *elem* is the root element. *loader* is
822+
an optional resource loader. If omitted, it defaults to :func:`default_loader`.
823+
If given, it should be a callable that implements the same interface as
824+
:func:`default_loader`. Returns the expanded resource. If the parse mode is
825+
``"xml"``, this is an ElementTree instance. If the parse mode is "text",
826+
this is a Unicode string. If the loader fails, it can return None or
827+
raise an exception.
828+
829+
741830
.. _elementtree-element-objects:
742831

743832
Element Objects

Doc/tools/susp-ignored.csv

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

0 commit comments

Comments
 (0)