Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Using lxml to parse the Microsoft API translation result from the xml
from lxml import etree
def get_text_from_msmt_xml (xml):
"""Parse the xml string returned by the MS machine translation API, and return just the text"""
text = []
doc = etree.fromstring(xml)
for elem in doc.xpath('/foo:string', namespaces={'foo': ''}):
if elem.text:
elem_text = ' '.join(elem.text.split())
if len(elem_text) > 0:
return ' '.join(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment