Hi, companion today we will see obviously the thing we will find in the article Python 3 Content to Switch XML over completely to HTML Report in Order Line Utilizing XSLT Library. We want to believe that you find this article exceptionally helpful.
Python 3 Script to Convert XML to HTML Document in Command Line Using XSLT Library
<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> lxml.etree
<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> lxml.html
>>>
<span class="hljs-meta">>>> </span>xmlstring = <span class="hljs-string">'''\
<span class="hljs-meta">... </span><?xml version='1.0' encoding='ASCII'?>
<span class="hljs-meta">... </span><root><a class="here">link1</a><a class="there">link2</a></root>
<span class="hljs-meta">... </span>'''</span>
<span class="hljs-meta">>>> </span>root = lxml.etree.fromstring(xmlstring)
<span class="hljs-meta">>>> </span>root.cssselect(<span class="hljs-string">'a.here'</span>)
Traceback (most recent call last):
File <span class="hljs-string">"<stdin>"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> <module>
AttributeError: <span class="hljs-string">'lxml.etree._Element'</span> <span class="hljs-built_in">object</span> has no attribute <span class="hljs-string">'cssselect'</span>
<span class="hljs-meta">>>> </span>root = lxml.html.fromstring(lxml.etree.tostring(root))
<span class="hljs-meta">>>> </span>root.cssselect(<span class="hljs-string">'a.here'</span>)
[<Element a at <span class="hljs-number">0x2989308</span>>]
<span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span> lxml.etree.tostring(root, xml_declaration=<span class="hljs-literal">True</span>)
<?xml version=<span class="hljs-string">'1.0'</span> encoding=<span class="hljs-string">'ASCII'</span>?>
<root><a <span class="hljs-keyword">class</span>=<span class="hljs-string">"here"</span>>link1</a><a <span class="hljs-keyword">class</span>=<span class="hljs-string">"there"</span>>link2</a></root>
Final Words
Python 3 Content to Switch XML over completely to HTML Report in Order Line Utilizing XSLT Library We truly want to believe that you find the article valuable and will see you in the following article.