-
Notifications
You must be signed in to change notification settings - Fork 142
/
xincluder.py
executable file
·35 lines (30 loc) · 1.01 KB
/
xincluder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
#
# Natural Language Toolkit: Process the XIncludes of an XML document
#
# Copyright (C) 2001-2012 NLTK Project
# Author: Steven Bird <[email protected]>
# URL: <http://www.nltk.org/>
# For license information, see LICENSE.TXT
import sys
import re
EXT = "-flat" # output filename extension
XI1 = r'<xi:include href="(.*?)".*/>'
DOC = r'(?s)<!DOCTYPE .*?>'
NAMESPACE = r' xmlns:xi="http://www.w3.org/2001/XInclude"'
for filename in sys.argv[1:]:
basename, suffix = filename.split('.')
output_filename = basename + EXT + "." + suffix
output = open(output_filename, "w")
for line in open(filename):
m = re.search(XI1, line)
if m:
contents = open(m.group(1)).read()
if re.search(DOC, contents):
contents = re.split(DOC, contents)[1]
output.write(contents)
else:
if NAMESPACE in line:
line = re.sub(NAMESPACE, '', line)
output.write(line)
output.close()