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

Skip to content

Commit 195dc14

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

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
@@ -640,6 +640,95 @@ Functions
640640
:class:`Element` instance and a dictionary.
641641

642642

643+
.. _elementtree-xinclude:
644+
645+
XInclude support
646+
----------------
647+
648+
This module provides limited support for
649+
`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.
650+
651+
Example
652+
^^^^^^^
653+
654+
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.
655+
656+
.. code-block:: xml
657+
658+
<?xml version="1.0"?>
659+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
660+
<xi:include href="source.xml" parse="xml" />
661+
</document>
662+
663+
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.
664+
665+
To process this file, load it as usual, and pass the root element to the :mod:`xml.etree.ElementTree` module:
666+
667+
.. code-block:: python
668+
669+
from xml.etree import ElementTree, ElementInclude
670+
671+
tree = ElementTree.parse("document.xml")
672+
root = tree.getroot()
673+
674+
ElementInclude.include(root)
675+
676+
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:
677+
678+
.. code-block:: xml
679+
680+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
681+
<para>This is a paragraph.</para>
682+
</document>
683+
684+
If the **parse** attribute is omitted, it defaults to "xml". The href attribute is required.
685+
686+
To include a text document, use the ``{http://www.w3.org/2001/XInclude}include`` element, and set the **parse** attribute to "text":
687+
688+
.. code-block:: xml
689+
690+
<?xml version="1.0"?>
691+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
692+
Copyright (c) <xi:include href="year.txt" parse="text" />.
693+
</document>
694+
695+
The result might look something like:
696+
697+
.. code-block:: xml
698+
699+
<document xmlns:xi="http://www.w3.org/2001/XInclude">
700+
Copyright (c) 2003.
701+
</document>
702+
703+
Reference
704+
---------
705+
706+
.. _elementinclude-functions:
707+
708+
Functions
709+
^^^^^^^^^
710+
711+
.. function:: xml.etree.ElementInclude.default_loader( href, parse, encoding=None)
712+
713+
Default loader. This default loader reads an included resource from disk. *href* is a URL.
714+
*parse* is for parse mode either "xml" or "text". *encoding*
715+
is an optional text encoding. If not given, encoding is ``utf-8``. Returns the
716+
expanded resource. If the parse mode is ``"xml"``, this is an ElementTree
717+
instance. If the parse mode is "text", this is a Unicode string. If the
718+
loader fails, it can return None or raise an exception.
719+
720+
721+
.. function:: xml.etree.ElementInclude.include( elem, loader=None)
722+
723+
This function expands XInclude directives. *elem* is the root element. *loader* is
724+
an optional resource loader. If omitted, it defaults to :func:`default_loader`.
725+
If given, it should be a callable that implements the same interface as
726+
:func:`default_loader`. Returns the expanded resource. If the parse mode is
727+
``"xml"``, this is an ElementTree instance. If the parse mode is "text",
728+
this is a Unicode string. If the loader fails, it can return None or
729+
raise an exception.
730+
731+
643732
.. _elementtree-element-objects:
644733

645734
Element Objects

Doc/tools/susp-ignored.csv

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

0 commit comments

Comments
 (0)