Consider also XSLT, the special-purpose language designed to transform XML files, which can retrieve nodes from a different XML file using document()
function. Additionally, you have better control of output including indentation and line breaks, headers, etc. Python's lxml
can run XSLT 1.0 scripts. Doing so you avoid any application layer nested looping.
XSLT (save as .xsl file, to be used in Python below)
Notice reference to other .xml file. Both XML files are assumed to be in same directory.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="vehicleDefinitions">
<xsl:copy>
<xsl:copy-of select="vehicleType"/>
<xsl:for-each select="document('schedule_mapped.xml')/descendant::departure">
<vehicle id="{@vehicleRefId}"
type="{../preceding-sibling::transportMode}"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Python
from lxml import etree
doc = etree.parse('vehicle.xml')
xsl = etree.parse('script.xsl')
transformer = etree.XSLT(xsl)
result = transformer(doc)
with open('Output.xml', 'wb') as f:
f.write(result)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…