source: exist/trunk/python/elementtree-1.3a6-20070312-badc/unittests/test_xinclude.py @ 3638

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/elementtree-1.3a6-20070312-badc/unittests/test_xinclude.py@3638
Revision 3638, 4.0 KB checked in by pjkersha, 13 years ago (diff)

Latest ET snapshot includes fix to Exclusive C14N inclusive namespace functionality

Line 
1# $Id: test_xinclude.py 3298 2007-09-13 14:38:02Z fredrik $
2
3from support import *
4from elementtree import ElementInclude
5
6#
7# xinclude tests (samples from appendix C of the xinclude specification)
8
9XINCLUDE = {}
10
11XINCLUDE["C1.xml"] = """\
12<?xml version='1.0'?>
13<document xmlns:xi="http://www.w3.org/2001/XInclude">
14  <p>120 Mz is adequate for an average home user.</p>
15  <xi:include href="disclaimer.xml"/>
16</document>
17"""
18
19XINCLUDE["disclaimer.xml"] = """\
20<?xml version='1.0'?>
21<disclaimer>
22  <p>The opinions represented herein represent those of the individual
23  and should not be interpreted as official policy endorsed by this
24  organization.</p>
25</disclaimer>
26"""
27
28XINCLUDE["C2.xml"] = """\
29<?xml version='1.0'?>
30<document xmlns:xi="http://www.w3.org/2001/XInclude">
31  <p>This document has been accessed
32  <xi:include href="count.txt" parse="text"/> times.</p>
33</document>
34"""
35
36XINCLUDE["count.txt"] = "324387"
37
38XINCLUDE["C3.xml"] = """\
39<?xml version='1.0'?>
40<document xmlns:xi="http://www.w3.org/2001/XInclude">
41  <p>The following is the source of the "data.xml" resource:</p>
42  <example><xi:include href="data.xml" parse="text"/></example>
43</document>
44"""
45
46XINCLUDE["data.xml"] = """\
47<?xml version='1.0'?>
48<data>
49  <item><![CDATA[Brooks & Shields]]></item>
50</data>
51"""
52
53XINCLUDE["C5.xml"] = """\
54<?xml version='1.0'?>
55<div xmlns:xi="http://www.w3.org/2001/XInclude">
56  <xi:include href="example.txt" parse="text">
57    <xi:fallback>
58      <xi:include href="fallback-example.txt" parse="text">
59        <xi:fallback><a href="mailto:bob@example.org">Report error</a></xi:fallback>
60      </xi:include>
61    </xi:fallback>
62  </xi:include>
63</div>
64"""
65
66XINCLUDE["default.xml"] = """\
67<?xml version='1.0'?>
68<document xmlns:xi="http://www.w3.org/2001/XInclude">
69  <p>Example.</p>
70  <xi:include href="samples/simple.xml"/>
71</document>
72"""
73
74def xinclude_loader(href, parse="xml", encoding=None):
75    try:
76        data = XINCLUDE[href]
77    except KeyError:
78        raise IOError("resource not found")
79    if parse == "xml":
80        return ET.XML(data)
81    return data
82
83def xinclude():
84    r"""
85    Basic inclusion example (XInclude C.1)
86
87    >>> document = xinclude_loader("C1.xml")
88    >>> ElementInclude.include(document, xinclude_loader)
89    >>> print serialize(document) # C1
90    <document>
91      <p>120 Mz is adequate for an average home user.</p>
92      <disclaimer>
93      <p>The opinions represented herein represent those of the individual
94      and should not be interpreted as official policy endorsed by this
95      organization.</p>
96    </disclaimer>
97    </document>
98
99    Textual inclusion example (XInclude C.2)
100
101    >>> document = xinclude_loader("C2.xml")
102    >>> ElementInclude.include(document, xinclude_loader)
103    >>> print serialize(document) # C2
104    <document>
105      <p>This document has been accessed
106      324387 times.</p>
107    </document>
108
109    Textual inclusion of XML example (XInclude C.3)
110
111    >>> document = xinclude_loader("C3.xml")
112    >>> ElementInclude.include(document, xinclude_loader)
113    >>> print serialize(document) # C3
114    <document>
115      <p>The following is the source of the "data.xml" resource:</p>
116      <example>&lt;?xml version='1.0'?&gt;
117    &lt;data&gt;
118      &lt;item&gt;&lt;![CDATA[Brooks &amp; Shields]]&gt;&lt;/item&gt;
119    &lt;/data&gt;
120    </example>
121    </document>
122
123    Fallback example (XInclude C.5)
124    Note! Fallback support is not yet implemented
125
126    >>> document = xinclude_loader("C5.xml")
127    >>> ElementInclude.include(document, xinclude_loader)
128    Traceback (most recent call last):
129    IOError: resource not found
130    >>> # print serialize(document) # C5
131
132    """
133
134def xinclude_default():
135    """
136    >>> document = xinclude_loader("default.xml")
137    >>> ElementInclude.include(document)
138    >>> print serialize(document) # default
139    <document>
140      <p>Example.</p>
141      <root>
142       <element key="value">text</element>
143       <element>text</element>tail
144       <empty-element />
145    </root>
146    </document>
147    """
Note: See TracBrowser for help on using the repository browser.