File: lxml.builder.html

package info (click to toggle)
lxml 6.0.2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 32,588 kB
  • sloc: python: 30,649; javascript: 1,640; ansic: 311; xml: 267; makefile: 243; sh: 68
file content (250 lines) | stat: -rw-r--r-- 19,861 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250


<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="./">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>lxml.builder module &mdash; lxml  documentation</title>
      <link rel="stylesheet" type="text/css" href="_static/pygments.css?v=b86133f3" />
      <link rel="stylesheet" type="text/css" href="_static/css/theme.css?v=e59714d7" />

  
      <script src="_static/jquery.js?v=5d32c60e"></script>
      <script src="_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
      <script src="_static/documentation_options.js?v=5929fcd5"></script>
      <script src="_static/doctools.js?v=9bcbadda"></script>
      <script src="_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="_static/js/theme.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="lxml.cssselect module" href="lxml.cssselect.html" />
    <link rel="prev" title="lxml._elementpath module" href="lxml._elementpath.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="index.html" class="icon icon-home">
            lxml
              <img src="_static/python-xml.png" class="logo" alt="Logo"/>
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="lxml.html">lxml package</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="lxml.html.html">lxml.html package</a><ul>
<li class="toctree-l3"><a class="reference internal" href="lxml.html.ElementSoup.html">lxml.html.ElementSoup module</a></li>
<li class="toctree-l3"><a class="reference internal" href="lxml.html.builder.html">lxml.html.builder module</a></li>
<li class="toctree-l3"><a class="reference internal" href="lxml.html.clean.html">lxml.html.clean module</a></li>
<li class="toctree-l3"><a class="reference internal" href="lxml.html.defs.html">lxml.html.defs module</a></li>
<li class="toctree-l3"><a class="reference internal" href="lxml.html.diff.html">lxml.html.diff module</a></li>
<li class="toctree-l3"><a class="reference internal" href="lxml.html.formfill.html">lxml.html.formfill module</a></li>
<li class="toctree-l3"><a class="reference internal" href="lxml.html.html5parser.html">lxml.html.html5parser module</a></li>
<li class="toctree-l3"><a class="reference internal" href="lxml.html.soupparser.html">lxml.html.soupparser module</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="lxml.isoschematron.html">lxml.isoschematron package</a></li>
<li class="toctree-l2"><a class="reference internal" href="lxml.ElementInclude.html">lxml.ElementInclude module</a></li>
<li class="toctree-l2"><a class="reference internal" href="lxml._elementpath.html">lxml._elementpath module</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">lxml.builder module</a></li>
<li class="toctree-l2"><a class="reference internal" href="lxml.cssselect.html">lxml.cssselect module</a></li>
<li class="toctree-l2"><a class="reference internal" href="lxml.doctestcompare.html">lxml.doctestcompare module</a></li>
<li class="toctree-l2"><a class="reference internal" href="lxml.etree.html">lxml.etree module</a></li>
<li class="toctree-l2"><a class="reference internal" href="lxml.objectify.html">lxml.objectify module</a></li>
<li class="toctree-l2"><a class="reference internal" href="lxml.sax.html">lxml.sax module</a></li>
</ul>
</li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">lxml</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="lxml.html">lxml package</a></li>
      <li class="breadcrumb-item active">lxml.builder module</li>
      <li class="wy-breadcrumbs-aside">
            <a href="_sources/lxml.builder.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="module-lxml.builder">
<span id="lxml-builder-module"></span><h1>lxml.builder module<a class="headerlink" href="#module-lxml.builder" title="Link to this heading"></a></h1>
<p>The <code class="docutils literal notranslate"><span class="pre">E</span></code> Element factory for generating XML documents.</p>
<dl class="py class">
<dt class="sig sig-object py" id="lxml.builder.ElementMaker">
<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">lxml.builder.</span></span><span class="sig-name descname"><span class="pre">ElementMaker</span></span><a class="headerlink" href="#lxml.builder.ElementMaker" title="Link to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>Element generator factory.</p>
<p>Unlike the ordinary Element factory, the E factory allows you to pass in
more than just a tag and some optional attributes; you can also pass in
text and other elements.  The text is added as either text or tail
attributes, and elements are inserted at the right spot.  Some small
examples:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span><span class="w"> </span><span class="nn">lxml</span><span class="w"> </span><span class="kn">import</span> <span class="n">etree</span> <span class="k">as</span> <span class="n">ET</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span><span class="w"> </span><span class="nn">lxml.builder</span><span class="w"> </span><span class="kn">import</span> <span class="n">E</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">E</span><span class="p">(</span><span class="s2">&quot;tag&quot;</span><span class="p">))</span>
<span class="go">&#39;&lt;tag/&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">E</span><span class="p">(</span><span class="s2">&quot;tag&quot;</span><span class="p">,</span> <span class="s2">&quot;text&quot;</span><span class="p">))</span>
<span class="go">&#39;&lt;tag&gt;text&lt;/tag&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">E</span><span class="p">(</span><span class="s2">&quot;tag&quot;</span><span class="p">,</span> <span class="s2">&quot;text&quot;</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="s2">&quot;value&quot;</span><span class="p">))</span>
<span class="go">&#39;&lt;tag key=&quot;value&quot;&gt;text&lt;/tag&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">E</span><span class="p">(</span><span class="s2">&quot;tag&quot;</span><span class="p">,</span> <span class="n">E</span><span class="p">(</span><span class="s2">&quot;subtag&quot;</span><span class="p">,</span> <span class="s2">&quot;text&quot;</span><span class="p">),</span> <span class="s2">&quot;tail&quot;</span><span class="p">))</span>
<span class="go">&#39;&lt;tag&gt;&lt;subtag&gt;text&lt;/subtag&gt;tail&lt;/tag&gt;&#39;</span>
</pre></div>
</div>
<p>For simple tags, the factory also allows you to write <code class="docutils literal notranslate"><span class="pre">E.tag(...)</span></code> instead
of <code class="docutils literal notranslate"><span class="pre">E('tag',</span> <span class="pre">...)</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">E</span><span class="o">.</span><span class="n">tag</span><span class="p">())</span>
<span class="go">&#39;&lt;tag/&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">E</span><span class="o">.</span><span class="n">tag</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">))</span>
<span class="go">&#39;&lt;tag&gt;text&lt;/tag&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">E</span><span class="o">.</span><span class="n">tag</span><span class="p">(</span><span class="n">E</span><span class="o">.</span><span class="n">subtag</span><span class="p">(</span><span class="s2">&quot;text&quot;</span><span class="p">),</span> <span class="s2">&quot;tail&quot;</span><span class="p">))</span>
<span class="go">&#39;&lt;tag&gt;&lt;subtag&gt;text&lt;/subtag&gt;tail&lt;/tag&gt;&#39;</span>
</pre></div>
</div>
<p>Here’s a somewhat larger example; this shows how to generate HTML
documents, using a mix of prepared factory functions for inline elements,
nested <code class="docutils literal notranslate"><span class="pre">E.tag</span></code> calls, and embedded XHTML fragments:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># some common inline elements</span>
<span class="n">A</span> <span class="o">=</span> <span class="n">E</span><span class="o">.</span><span class="n">a</span>
<span class="n">I</span> <span class="o">=</span> <span class="n">E</span><span class="o">.</span><span class="n">i</span>
<span class="n">B</span> <span class="o">=</span> <span class="n">E</span><span class="o">.</span><span class="n">b</span>

<span class="k">def</span><span class="w"> </span><span class="nf">CLASS</span><span class="p">(</span><span class="n">v</span><span class="p">):</span>
    <span class="c1"># helper function, &#39;class&#39; is a reserved word</span>
    <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="n">v</span><span class="p">}</span>

<span class="n">page</span> <span class="o">=</span> <span class="p">(</span>
    <span class="n">E</span><span class="o">.</span><span class="n">html</span><span class="p">(</span>
        <span class="n">E</span><span class="o">.</span><span class="n">head</span><span class="p">(</span>
            <span class="n">E</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">&quot;This is a sample document&quot;</span><span class="p">)</span>
        <span class="p">),</span>
        <span class="n">E</span><span class="o">.</span><span class="n">body</span><span class="p">(</span>
            <span class="n">E</span><span class="o">.</span><span class="n">h1</span><span class="p">(</span><span class="s2">&quot;Hello!&quot;</span><span class="p">,</span> <span class="n">CLASS</span><span class="p">(</span><span class="s2">&quot;title&quot;</span><span class="p">)),</span>
            <span class="n">E</span><span class="o">.</span><span class="n">p</span><span class="p">(</span><span class="s2">&quot;This is a paragraph with &quot;</span><span class="p">,</span> <span class="n">B</span><span class="p">(</span><span class="s2">&quot;bold&quot;</span><span class="p">),</span> <span class="s2">&quot; text in it!&quot;</span><span class="p">),</span>
            <span class="n">E</span><span class="o">.</span><span class="n">p</span><span class="p">(</span><span class="s2">&quot;This is another paragraph, with a &quot;</span><span class="p">,</span>
                <span class="n">A</span><span class="p">(</span><span class="s2">&quot;link&quot;</span><span class="p">,</span> <span class="n">href</span><span class="o">=</span><span class="s2">&quot;http://www.python.org&quot;</span><span class="p">),</span> <span class="s2">&quot;.&quot;</span><span class="p">),</span>
            <span class="n">E</span><span class="o">.</span><span class="n">p</span><span class="p">(</span><span class="s2">&quot;Here are some reserved characters: &lt;spam&amp;egg&gt;.&quot;</span><span class="p">),</span>
            <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s2">&quot;&lt;p&gt;And finally, here is an embedded XHTML fragment.&lt;/p&gt;&quot;</span><span class="p">),</span>
        <span class="p">)</span>
    <span class="p">)</span>
<span class="p">)</span>

<span class="nb">print</span> <span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">page</span><span class="p">)</span>
</pre></div>
</div>
<p>Here’s a prettyprinted version of the output from the above script:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;This is a sample document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1 class=&quot;title&quot;&gt;Hello!&lt;/h1&gt;
    &lt;p&gt;This is a paragraph with &lt;b&gt;bold&lt;/b&gt; text in it!&lt;/p&gt;
    &lt;p&gt;This is another paragraph, with &lt;a href=&quot;http://www.python.org&quot;&gt;link&lt;/a&gt;.&lt;/p&gt;
    &lt;p&gt;Here are some reserved characters: &amp;lt;spam&amp;amp;egg&amp;gt;.&lt;/p&gt;
    &lt;p&gt;And finally, here is an embedded XHTML fragment.&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre></div>
</div>
<p>For namespace support, you can pass a namespace map (<code class="docutils literal notranslate"><span class="pre">nsmap</span></code>)
and/or a specific target <code class="docutils literal notranslate"><span class="pre">namespace</span></code> to the ElementMaker class:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">E</span> <span class="o">=</span> <span class="n">ElementMaker</span><span class="p">(</span><span class="n">namespace</span><span class="o">=</span><span class="s2">&quot;http://my.ns/&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span> <span class="n">E</span><span class="o">.</span><span class="n">test</span> <span class="p">))</span>
<span class="go">&lt;test xmlns=&quot;http://my.ns/&quot;/&gt;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">E</span> <span class="o">=</span> <span class="n">ElementMaker</span><span class="p">(</span><span class="n">namespace</span><span class="o">=</span><span class="s2">&quot;http://my.ns/&quot;</span><span class="p">,</span> <span class="n">nsmap</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;p&#39;</span><span class="p">:</span><span class="s1">&#39;http://my.ns/&#39;</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span> <span class="n">E</span><span class="o">.</span><span class="n">test</span> <span class="p">))</span>
<span class="go">&lt;p:test xmlns:p=&quot;http://my.ns/&quot;/&gt;</span>
</pre></div>
</div>
<dl class="py attribute">
<dt class="sig sig-object py" id="lxml.builder.ElementMaker._makeelement">
<span class="sig-name descname"><span class="pre">_makeelement</span></span><a class="headerlink" href="#lxml.builder.ElementMaker._makeelement" title="Link to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="lxml.builder.ElementMaker._namespace">
<span class="sig-name descname"><span class="pre">_namespace</span></span><a class="headerlink" href="#lxml.builder.ElementMaker._namespace" title="Link to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="lxml.builder.ElementMaker._nsmap">
<span class="sig-name descname"><span class="pre">_nsmap</span></span><a class="headerlink" href="#lxml.builder.ElementMaker._nsmap" title="Link to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="lxml.builder.ElementMaker._typemap">
<span class="sig-name descname"><span class="pre">_typemap</span></span><a class="headerlink" href="#lxml.builder.ElementMaker._typemap" title="Link to this definition"></a></dt>
<dd></dd></dl>

</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="lxml.builder.__pyx_unpickle_ElementMaker">
<span class="sig-prename descclassname"><span class="pre">lxml.builder.</span></span><span class="sig-name descname"><span class="pre">__pyx_unpickle_ElementMaker</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">__pyx_type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">__pyx_checksum</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">__pyx_state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lxml.builder.__pyx_unpickle_ElementMaker" title="Link to this definition"></a></dt>
<dd></dd></dl>

</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="lxml._elementpath.html" class="btn btn-neutral float-left" title="lxml._elementpath module" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="lxml.cssselect.html" class="btn btn-neutral float-right" title="lxml.cssselect module" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2020, lxml dev team.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>