I need to convert an XML document to LaTeX. Something like
<?xml version="1.0" encoding="UTF-8"?>
<foo>
12345
<bar>
67890
</bar>
</foo>
to
\foo{12345\bar{67890}}
If I do
from bs4 import BeautifulSoup
with open("foobar.xml") as fp:
soup = BeautifulSoup(fp, 'xml')
for tag in soup.find_all("foo"):
tag.replaceWith(f"""{tag.text}""")
then it removes the tags inside. If I use contents instead of text, it returns a list.
I have tried soup.find('foo').unwrap(), but it just removes the tag, without replacing it.
I know I can first replace bar and then foo, but I would like a solution that doesn't depend on the order of the tags.