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