3 from sys import argv, stdout, stderr
8 stdout = codecs.getwriter('utf-8')(stdout)
10 NS_XI = 'http://www.w3.org/2001/XInclude'
12 def xincludate(dom, base, dropns = []):
14 for i in xrange(dom.documentElement.attributes.length):
15 attr = dom.documentElement.attributes.item(i)
16 if attr.prefix == 'xmlns':
17 if attr.localName in dropns:
18 remove_attrs.append(attr)
20 dropns.append(attr.localName)
21 for attr in remove_attrs:
22 dom.documentElement.removeAttributeNode(attr)
23 for include in dom.getElementsByTagNameNS(NS_XI, 'include'):
24 href = include.getAttribute('href')
25 # FIXME: assumes Unixy paths
26 filename = os.path.join(os.path.dirname(base), href)
27 subdom = xml.dom.minidom.parse(filename)
28 xincludate(subdom, filename, dropns)
30 subdom.documentElement.setAttribute('xml:base', href)
31 include.parentNode.replaceChild(subdom.documentElement, include)
33 if __name__ == '__main__':
35 dom = xml.dom.minidom.parse(argv[0])
36 xincludate(dom, argv[0])