Although, if (and I’m guessing here) <w> represents a word, and <l> is a line, and <pc> is punctuation, you might want to insert different separators after element text, in which case, this might be simpler done in XSLT or XQuery than in Python/lxml .
On Sep 14, 2020, at 5:32 PM, Majewski, Steven Dennis (sdm7g) <sdm7g@virginia.edu> wrote:
You could do something like:
' '.join([ s.strip() for s in etree.parse('/tmp/x.xml').xpath( '//*/text()' ) ] ) ' and therein take delight : ‘
Or you can try using the parser option to remove whitespace text nodes:
etree.parse( '/tmp/x.xml', etree.XMLParser( remove_blank_text = True ) ).xpath( '//*/text()' ) ['and', 'therein', 'take', 'delight', ':’]
— Steve M.
On Sep 14, 2020, at 4:50 PM, Martin Mueller <martinmueller@northwestern.edu> wrote:
I have texts that are linguistically and every token is wrapped in a <w> or <pc> element, like so:
<l xml:id="c0abe-e140"> <w lemma="and" pos="cc" xml:id="c0abe-001-a-0670">and</w> <w lemma="therein" pos="av" xml:id="c0abe-001-a-0680">therein</w> <w lemma="take" pos="vvb" xml:id="c0abe-001-a-0690">take</w> <w lemma="delight" pos="n1" xml:id="c0abe-001-a-0700">delight</w> <pc xml:id="c0abe-001-a-0710">:</pc> </l>
I'd like to strip the annotation and generate a plain file in which the <w> and <pc> elements are replaced by just their content. Some years ago I used the strip_tags method, and I remember it working. By this I mean that the words in a line of text appeared with a space between words. But when I use it now, it does strip the tags but the text appears verticalized, with one token on each line. I've tried various methods of getting rid of the line break, but none of them work.
For instance, I used this bit of code to replace the linebreak with nothing:
for element in tree.iter('*'):
plaintext = element.text if plaintext is not None: plaintext = plaintext.replace('\n', '')
This does something: if I print out each element, the line breaks go away. But if I then print out the whole file, using
print(etree.tostring(tree, encoding = 'unicode', pretty_print=True)
the result is a verticalized text. Which means that the deletion of the line breaks at a later stage, but I don’t know how to do that.
I'll be grateful for advice
Martin Mueller Professor English and Classics Northwestern University
_________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml
_________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml