1

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);
Omar T
  • 123
  • 11

2 Answers2

0

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.

Henrick
  • 31
  • 1
  • 6
0

You can directly run this unix command from your php application in order to generate the file:

(head -1 xml1; xmllint -xpath '//JobRecord' xml2 xml1; tail -1 xml1) | xmllint --format - > newXML.xml

Demo:

more xml1 xml2
::::::::::::::
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>

$ (head -1 xml1; xmllint -xpath '//JobRecord' xml2 xml1; tail -1 xml1) | xmllint --format -
<?xml version="1.0"?>
<JobRecords>
  <JobRecord>
    <Brand>Corporate2</Brand>
    <WorkTypes>
      <WorkTypeRecord>
        <Title>Permanent2</Title>
      </WorkTypeRecord>
    </WorkTypes>
  </JobRecord>
  <JobRecord>
    <Brand>Corporate1</Brand>
    <WorkTypes>
      <WorkTypeRecord>
        <Title>Permanent1</Title>
      </WorkTypeRecord>
    </WorkTypes>
  </JobRecord>
</JobRecords>

It will be easier to use directly some xml parser with the correct xpath and reconstruct the output XML header/footer.

To run a shell command from your php application check this link: PHP shell_exec() vs exec()

Allan
  • 12,117
  • 3
  • 27
  • 51