使用XSLT将多个XML文件合并为一个XML文件

时间:2019-09-24 05:57:36

标签: python xml python-3.x xslt lxml

我正在尝试将XML位置作为参数从Python发送到XSLT,并尝试将其合并到邮件XML文件。 以下是所使用的元素树的代码

import os
import lxml.etree as ET

inputpath = r"C:\Users\[0].xml"
xsltfile = r"C:\Users\styleSheet.xsl"
outpath = r"C:\Users\Output.xml"

xml = ET.parse(inputpath)
xslt = ET.parse(xsltfile)

transform = ET.XSLT(xslt)
xmlLocation = ET.XSLT.strparam(r"C:\Users\Image.xml")
newdom = transform(xml, xml_path=xmlLocation)

tree_out = ET.tostring(newdom, encoding='UTF-8', pretty_print=True,  xml_declaration=True)
print(tree_out)

xmlfile = open(outpath ,'wb')
xmlfile.write(tree_out)
xmlfile.close()

下面是主要的XML文件

[0] .xml

<?xml version="1.0" encoding="UTF-8"?>
<ps:pt xmlns:ps="http://schemas.microsoft.com/windows/2003/psfrk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1" xmlns:ns0000="http://schemas.mta.com/1" xmlns:psk="http://schemas.microsoft.com/windows/2003/08">
    <ps:pi name="ns0000:PDS">
        <ps:Value xsi:type="xsd:string">
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE0AYQBuAHUAYQBsAAAAAAAAAAAAAAAAAAAAAAAAAAAA</ps:Value>
    </ps:pi>
    <ps:pi name="psk:JCAD">
        <ps:Value xsi:type="xsd:integer">1</ps:Value>
    </ps:pi>
</ps:pt>

styleSheet.xsl

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ps="http://schemas.microsoft.com/windows/2003/psfrk" version="1.0"> 
<xsl:param name="xml_path" /> 
    <xsl:template match="/">
        <xsl:copy>
            <xsl:copy-of select="ps:pt"/>
            <xsl:copy-of select="document($xml_path)/*"/>
        </xsl:copy>
    </xsl:template> 

</xsl:transform>

Image.xml

<?xml version="1.0" encoding="UTF-8"?>
<ps:pt xmlns:ps="http://schemas.microsoft.com/windows/2003/psfrk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1" xmlns:ns0000="http://schemas.mta.com/1" xmlns:psk="http://schemas.microsoft.com/windows/2003/08">
    <ps:f name="psk:PW">
        <ps:Option name="ns0000:Image">
            <ps:sp name="ns0000:File">
                <ps:pr name="ns0000:PWI"/>
            </ps:sp>
        </ps:f>
    </ps:Option>
</ps:pt>

必需的输出: Output.xml

<?xml version="1.0" encoding="UTF-8"?>
<ps:pt xmlns:ps="http://schemas.microsoft.com/windows/2003/psfrk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1" xmlns:ns0000="http://schemas.mta.com/1" xmlns:psk="http://schemas.microsoft.com/windows/2003/08">
    <ps:pi name="ns0000:PDS">
        <ps:Value xsi:type="xsd:string">
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE0AYQBuAHUAYQBsAAAAAAAAAAAAAAAAAAAAAAAAAAAA</ps:Value>
    </ps:pi>
    <ps:pi name="psk:JCAD">
        <ps:Value xsi:type="xsd:integer">1</ps:Value>
    </ps:pi>
    <ps:f name="psk:PW">
        <ps:Option name="ns0000:Image">
            <ps:sp name="ns0000:File">
                <ps:pr name="ns0000:PWI"/>
            </ps:sp>
        </ps:f>
    </ps:Option>
</ps:pt>

运行此代码时。我只在输出中看到[0] .xml。它不是两个XML的合并 我在哪里做错了什么。 XSLT是否有问题?

0 个答案:

没有答案