I need to parse some XML to pull out embedded template tags for further parsing. I can't seem to bend Python's regular expressions to do what I want, though.
In English: when a template tag is contained anywhere in the row, remove all the XML for that specific row and leave only the template tag in its place.
I put together a test case to demonstrate. Here's the original XML:
<!-- regex_trial.xml -->
<w:tbl>
<w:tr>
<w:tc><w:t>Header 1</w:t></w:tc>
<w:tc><w:t>Header 2</w:t></w:tc>
<w:tc><w:t>Header 3</w:t></w:tc>
</w:tr>
<w:tr>
<w:tc><w:t>{% for i in items %}</w:t></w:tc>
<w:tc><w:t></w:t></w:tc>
<w:tc><w:t></w:t></w:tc>
</w:tr>
<w:tr>
<w:tc><w:t>{{ i.field1 }}</w:t></w:tc>
<w:tc><w:t>{{ i.field2 }}</w:t></w:tc>
<w:tc><w:t>{{ i.field3 }}</w:t></w:tc>
</w:tr>
<w:tr>
<w:tc><w:t>{% endfor %}</w:t></w:tc>
<w:tc><w:t></w:t></w:tc>
<w:tc><w:t></w:t></w:tc>
</w:tr>
</w:tbl>
This is the desired result:
<!-- regex_desired_result.xml -->
<w:tbl>
<w:tr>
<w:tc><w:t>Header 1</w:t></w:tc>
<w:tc><w:t>Header 2</w:t></w:tc>
<w:tc><w:t>Header 3</w:t></w:tc>
</w:tr>
{% for i in items %}
<w:tr>
<w:tc><w:t>{{ i.field1 }}</w:t></w:tc>
<w:tc><w:t>{{ i.field2 }}</w:t></w:tc>
<w:tc><w:t>{{ i.field3 }}</w:t></w:tc>
</w:tr>
{% endfor %}
</w:tbl>
Here is some python code I am using to test:
#!/usr/bin/env python
import re
f = open( 'regex_trial.xml', 'r' )
orig_xml = f.read()
f.close()
p = re.compile( '<w:tr.*?(?P<tag>{%.*?%}).*?</w:tr>', re.DOTALL )
new_xml = p.sub( '\g<tag>', orig_xml, 0 )
print new_xml
The actual result of this regex is:
<!-- regex_trial.xml -->
<w:tbl>
{% for i in items %}
{% endfor %}
</w:tbl>
Any help is greatly appreciated! If we can figure this out, we will be able to dynamically generate MS Word docx files on the fly from Django-powered sites. Thanks!!
Update: this is the final code that I used
from xml.etree import ElementTree
import cStringIO as StringIO
TEMPLATE_TAG = 'template_text'
tree = ElementTree.parse( 'regex_trial.xml' )
rows = tree.getiterator('tr')
for row in rows:
for cell in row.getiterator('t'):
if cell.text and cell.text.find( '{%' ) >= 0:
template_tag = cell.text
row.clear()
row.tag = TEMPLATE_TAG
row.text = template_tag
break
output = StringIO.StringIO()
tree.write( output )
xml = output.getvalue()
xml = xml.replace('<%s>' % TEMPLATE_TAG, '')
xml = xml.replace('</%s>' % TEMPLATE_TAG, '')
print xml
Thanks for all the help!