drnpwmq4536 2018-03-09 01:25
浏览 549

将目录中的多个xml文件合并到一个文件中

I have multiple xml files in a website directory folder called "jobs" and ive been trying to get any xml file that within that jobs folder to combine into one xml file called Output.xml

The PHP code i have partly works however it my issue is it doesn't combine all the xml files in that jobs directory into the output.xml unless i specify the filename. Now im a bit stuck as i dont know why it only shows one in that output.xml.

Here some example code:

XML1

<JobRecords>
    <JobRecord>
        <Brand>Corporate1</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent1</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
</JobRecords>  

XML2

<JobRecords>
    <JobRecord>
        <Brand>Corporate2</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent2</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
</JobRecords>  

XSLT (save as .xsl file in same directory as all XML files)

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:strip-space elements="*"/>

        <xsl:template match="/">
            <files>
                <xsl:copy-of select="/jobs"/>               
                <!-- add more as needed -->
            </files>
        </xsl:template>

</xsl:stylesheet>

PHP (load first XML and XSL scripts, then transform/output)

// LOAD XML SOURCE
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->load('xml1.xml');                  

// LOAD XSL SOURCE
$xsl = new DOMDocument('1.0', 'UTF-8');
$xsl->load('XSLT_Script.xsl');

// TRANSFORM XML
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$newXML = $proc->transformToXML($xml);

// SAVE NEW XML
file_put_contents('output.xml', $newXML);
  • 写回答

2条回答 默认 最新

  • douchuopiao9002 2018-03-09 01:33
    关注

    I'm sure there is a cleaner way but you could use array scandir http://php.net/manual/en/function.scandir.php to get all the filenames in the directory (if there is anything other than xml files , you could filter them out) and then iterate through them in your function.

    评论

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM