JavaScript Python lxml library fails to parse < and > python lxml library,python lxml library install,lxml python library

I have an XSLT with javascript in it which uses "&lt ;" and "&gt ;" inside for loop

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
  <head> </head>
  <body>
    <script language="javascript" type="text/javascript">
  function example() {
        var trs = document.getElementsByTagName("tr");
    for (var i = 0; i &lt; trs.length; i++) {
    }
      }
     </script>
  </body>
</html>

I am using PYTHON LXML library to generate HTML using XSLT and XML.

import lxml.etree as ET
xml = ET.parse('sample.xml')
xslt = ET.parse('sample.xsl')
transform = ET.XSLT(xslt)
content = transform(xml)
f = open('output.html','w')
f.write(ET.tostring(content , pretty_print=True))
f.close()

But LXML is unable to replace special characters in the output HTML file

&lt ; to '<' and &gt ; to '>'

Is there any standard practice using LXML to replace "&lt ;" to '<' ?

To over come this issue I have to write another piece of code before writing to the file.

content = content.replace("&gt;", ">")
content = content.replace("&lt;", "<")
Answer:1

In order to decode/convert HTML entities, you should use method="html" in tostring() call:

ET.tostring(content, method="html", pretty_print=True)

or:

lxml.html.tostring(content, pretty_print=True)

DEMO:

from lxml import etree


text = """<html>
  <body>
    <script> 1 &lt; 2 </script>
  </body>
</html>
"""

tree = etree.fromstring(text)
print etree.tostring(tree, method="html")

prints:

<html>
  <body>
    <script> 1 < 2 </script>
  </body>
</html>
Answer:2

There are a list of links on a page and some of them are already visited by me. I have to make them invisible after any action(checking a checkbox, clicking button, etc). I tried to use jQuery: $("a:...

There are a list of links on a page and some of them are already visited by me. I have to make them invisible after any action(checking a checkbox, clicking button, etc). I tried to use jQuery: $("a:...

  1. hide visited links
  2. res hide visited links
  3. css hide visited links
  4. javascript hide visited links

Here is what i need to do. So i have this code <input type="file" id="uploadImage" name="image" /> <input type="submit" id="ImageName" name="submit" value="Submit"> So when i click ...

Here is what i need to do. So i have this code <input type="file" id="uploadImage" name="image" /> <input type="submit" id="ImageName" name="submit" value="Submit"> So when i click ...

  1. browser javascript
  2. browser javascript console
  3. browser javascript support
  4. browser javascript test
  5. browser javascript editor
  6. browser javascript engine
  7. browser javascript import
  8. browser javascript console script error
  9. browser javascript api
  10. browser javascript ide
  11. browser javascript modules
  12. browser javascript version support
  13. browser javascript compatibility
  14. browser javascript version
  15. browser javascript console chrome
  16. browser javascript console error
  17. browser javascript security model
  18. browser javascript error
  19. browser javascript interpreter
  20. browser javascript sandbox

I don't know javascript and I have been searching every where for this answer. I would like to duplicate content in my page. The html and content comes directly from a broker. The result wanted is : ...

I don't know javascript and I have been searching every where for this answer. I would like to duplicate content in my page. The html and content comes directly from a broker. The result wanted is : ...

  1. document.getelementbyid duplicate id

I have a polymer input field like this: <paper-input type="text" class="user-value" data-id="user-value" value="{{typingAns}}" focus maxlength="10" /> is it possible to block the backspace ...

I have a polymer input field like this: <paper-input type="text" class="user-value" data-id="user-value" value="{{typingAns}}" focus maxlength="10" /> is it possible to block the backspace ...