File indexing completed on 2024-04-28 16:13:14

0001 #!/usr/bin/python
0002 
0003 from sys import argv, stdout, stderr
0004 import codecs, locale
0005 import os
0006 import xml.dom.minidom
0007 
0008 stdout = codecs.getwriter('utf-8')(stdout)
0009 
0010 NS_XI = 'http://www.w3.org/2001/XInclude'
0011 
0012 def xincludate(dom, base, dropns = []):
0013     remove_attrs = []
0014     for i in xrange(dom.documentElement.attributes.length):
0015         attr = dom.documentElement.attributes.item(i)
0016         if attr.prefix == 'xmlns':
0017             if attr.localName in dropns:
0018                 remove_attrs.append(attr)
0019             else:
0020                 dropns.append(attr.localName)
0021     for attr in remove_attrs:
0022         dom.documentElement.removeAttributeNode(attr)
0023     for include in dom.getElementsByTagNameNS(NS_XI, 'include'):
0024         href = include.getAttribute('href')
0025         # FIXME: assumes Unixy paths
0026         filename = os.path.join(os.path.dirname(base), href)
0027         subdom = xml.dom.minidom.parse(filename)
0028         xincludate(subdom, filename, dropns)
0029         if './' in href:
0030             subdom.documentElement.setAttribute('xml:base', href)
0031         include.parentNode.replaceChild(subdom.documentElement, include)
0032 
0033 if __name__ == '__main__':
0034     argv = argv[1:]
0035     dom = xml.dom.minidom.parse(argv[0])
0036     xincludate(dom, argv[0])
0037     xml = dom.toxml()
0038     stdout.write(xml)
0039     stdout.write('\n')