Saturday, 15 June 2013

python - lxml: Converting XML to HTML through XSLT and get HtmlElements -


I have data that comes in the form of an XML file, I have also been given XSLT to convert XML to HTML I can use LXL to convert, however, I want to replace some HTML tags after conversion. How do I change this new entry into HTML Elements so that I can use some special methods like .cssselect () and so on.

  & gt; & Gt; & Gt; Import lxml.etree & gt; & Gt; & Gt; Import lxml.html & gt; & Gt; & Gt; & Gt; & Gt; & Gt; Xmlstring = '' '\ ... & lt ;? XML version = '1.0' encoding = 'ASCII'? & Gt; ... & lt; Root & gt; & Lt; One class = "here" & gt; Link1 & lt; / A & gt; & Lt; One class = "there" & gt; Link2 & lt; / A & gt; & Lt; / Root & gt; ... '' '& gt; & Gt; & Gt; Root = lxml.etree.fromstring (xmlstring)> gt; & Gt; & Gt; Root.cssselect ('a.here') traceback (last most recent call): File "& lt; stdin>", line 1, & lt; Module & gt; AttributeError: The 'lxml.etree._Element' object has no attribute in the 'cssselect'   

lxml.etree.tostring (root) - & gt; lxml.html .fromstring (..)

  & gt; & Gt; & Gt; Root = lxml.html.fromstring (lxml.etree.tostring) & gt; & Gt; & Gt; Original CSSCEct ('A.Yes') [& lt; Element A on 0x2989308> Get the XML output:  
  & gt; & Gt; & Gt; Print lxml.etree.tostring (root, xml_declaration = true) & lt ;? XML version = '1.0' encoding = 'ASCII'? & Gt; & Lt; Root & gt; & Lt; One class = "here" & gt; Link1 & lt; / A & gt; & Lt; One class = "there" & gt; Link2 & lt; / A & gt; & Lt; / Root & gt;    

No comments:

Post a Comment