如何使用PHP将xml文件合并为一个?

时间:2018-02-14 12:14:46

标签: php xslt

所以我在理解如何使用PHP将三个xml文件合并到一个xml文件中时遇到了一些麻烦。

我已经开始了,但我遇到了困难,首先是我的xsl将xml按国家/地区分为3组,文件名为“CD1_USA.xml”,CD2_UK UK UK.xml“和”CD2_AU AU.xml “:

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" name="cd-format"/>
<xsl:template match="/">
<xsl:for-each-group select="/CATALOG//CD" group-by="COUNTRY">
    <xsl:result-document href="CD{position()}_{current-group()/COUNTRY}.xml" format="cd-format">

    <CD_LIST country="{current-group()/COUNTRY}">
        <xsl:copy-of select="current-group()"/>
    </CD_LIST>

    </xsl:result-document>
</xsl:for-each-group>       
</xsl:template>
</xsl:stylesheet>

现在使用PHP我需要将这3个xml合并到一个名为“mergeFile.xml”

的文件中

所以我开始尝试但是我有点迷失在这里,因为mergeFile必须有序言

<?xml version="1.0" encoding="UTF-8"?>    

从这一点来看,有些指导意见

PHP

<?php
// Read file 1
$xmlfilecontent = file_get_contents('CD1_USA.xml');

// Concat file 2
$xmlfilecontent .= file_get_contents('CD2_UK UK UK.xml');

//Concat file 3
$xmlfilecontent .= file_get_contents('CD2_AU AU.xml');

1 个答案:

答案 0 :(得分:0)

您应该将数据存储到Array

<?php
    $xmlfilecontent = array();

    // Read and Insert file 1 into array
    $xmlfilecontent[] = file_get_contents('CD1_USA.xml');

    // Read and Insert file 2 into array
    $xmlfilecontent[] = file_get_contents('CD2_UK UK UK.xml');

    // Read and Insert file 3 into array
    $xmlfilecontent[] = file_get_contents('CD2_AU AU.xml');

    print_r($xmlfilecontent);
?>
相关问题