Adding data from one XML file to another - PHP

Question

Any help would be appreciated on this. Im looking to take a value from one XML file and input it into another XML using PHP preferably. I will accept any other methods of transformation if they work.

XML 1

<field-list>
<field Id="spMF_1_1634">
    <tag>Customer Name</tag>
    <type>string</type>
    <required>true</required>
    <value>1Test</value>
</field>
<field Id="spMF_2_1634">
    <tag>Job Reference</tag>
    <type>string</type>
    <required>false</required>
    <value>42442</value>
</field>

XML 2

<Log>
  <DataSource>Data_source</DataSource>
  <SQLStatement>SELECT TotalPrice FROM vwQuotations WHERE reference = 'REF42442'</SQLStatement>
  <SQLStatementResult>Success</SQLStatementResult>
  <SQLStatementValue>
    <Row>
      <Column Name="TotalPrice" DataType="REAL">2000</Column>
    </Row>
  </SQLStatementValue>
</Log>

OUTPUT / RESULT IM LOOKING FOR:

<field-list>
<field Id="spMF_1_1634">
    <tag>Customer Name</tag>
    <type>string</type>
    <required>true</required>
    <value>1Test</value>
</field>
<field Id="spMF_2_1634">
    <tag>Job Reference</tag>
    <type>string</type>
    <required>false</required>
    <value>42442</value>
</field>
<field Id="AnyID">
    <tag>TotalPrice</tag>
    <type>string</type>
    <required>false</required>
    <value>2000</value>
</field>

The Value of the third field (2000) is really what i require added to the first XML.

As you can see the two XML are in different formats. I hope I have explained myself clearly.

To summarise I'm looking to add an extra field to XML 1 with the child properties to match 2000 from XML 2 and output in the same format as XML 1 as per my output example.

Thank you.

******EDIT******

I'm using automation software to handle these files so unfortunately I'm unable to pass parameters to the XML.

I'm striping XML off a PDF (XML 1) and I can pass it through a XSLT transform module to export to a CSV.

XML 2 is pulled from a database and can be held in a static folder.

Please see below screenshot with annotations.

Can I hardcode the XML 2 File path into the XLT Transform to insert the data?

enter image description here


Show source
| php   | xml   | xslt   | scripting   2016-12-09 10:12 1 Answers

Answers ( 1 )

  1. 2016-12-09 10:12

    You can tell your processor to process the first XML and pass the path to the second XML as a parameter. Then use a stylesheet like:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:param name="path-to-file2"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="field-list">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <field Id="AnyID">
                <tag>TotalPrice</tag>
                <type>string</type>
                <required>false</required>
                <value>
                    <xsl:value-of select="document($path-to-file2)/Log/SQLStatementValue/Row/Column"/>
                </value>
            </field>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    
◀ Go back