0

我正在开发一个应用程序,它将采用 2 个 XML 文件并合并为 1 个。目前我可以生成一个 XML 文件,但它的格式不是我想要的,而且我现在已经碰壁了。

  1. 应用程序读取 2 个 XML 文件,然后从其中一个 XML 文件中检查每个员工记录。
  2. 对于第一个 XML 文件中的每个员工记录
    1. 它在第二个 XML 中查找相应的记录
    2. 一旦被发现,它会生成一系列新节点并使用第二个 XML 文件中的信息填充它

我想将这些新节点放置在特定点的相关人员记录中(目前它只是将它们全部转储到 XML 文档的底部)

我遇到的问题是从第二个 xml 中获取相应的数据,以便与第一个 xml 文件中的数据一起放置。如您所见,它只是放在文档的底部,并没有链接到相关记录(生成的输出),数据应该出现在上面,我目前不知道如何做到这一点,任何建议都将不胜感激。谢谢

PHP的

<?php

$schoolExtract = new DOMDocument;
$schoolExtract->load('schoolExtract.xml');

$kcsExtract = new DOMDocument;
$kcsExtract->preserveWhiteSpace = false;
$kcsExtract->load('kcsFinalExtract.xml');
$xp = new DOMXPath($kcsExtract);

header("Content-Type: text/plain");

$kcsUserRecord = $kcsExtract->getElementsByTagName( "StaffDetails" ); 

$schoolRecord = $schoolExtract->getElementsByTagName( "StaffDetails" );

$schoolRecordCount = $schoolExtract->getElementsByTagName( "SchoolWorkforceMember" );

$noRecords = $schoolRecordCount->length;

set_time_limit(0);

$i = 0;

foreach($schoolRecord as $row2)
{

    $foundRecord = 0;

    $NationalInsuranceSE = $row2->getElementsByTagName("NINumber")->item(0)->nodeValue;
    $StaffMemberNameSE = $row2->getElementsByTagName("PersonFamilyName")->item(0)->nodeValue;
    $PersonGivenNameSE = $row2->getElementsByTagName("PersonGivenName")->item(0)->nodeValue;
    $DOBSE = $row2->getElementsByTagName("PersonBirthDate")->item(0)->nodeValue;

    $newDOBSE = date("d/m/Y", strtotime($DOBSE));

    foreach($kcsUserRecord as $row4)
    {
        $NationalInsuranceKCS = $row4->getElementsByTagName("NationalInsurance")->item(0)->nodeValue;
        $StaffMemberNameKCS = $row4->getElementsByTagName("StaffMemberName")->item(0)->nodeValue;
        $DOBKCS = $row4->getElementsByTagName("PersonBirthDate")->item(0)->nodeValue;

        if($NationalInsuranceSE == $NationalInsuranceKCS && $StaffMemberNameSE == $StaffMemberNameKCS){

            $foundRecord = 1;

            $xpQuery = $xp->query('/KCSExtract/StaffRecord[StaffDetails/NationalInsurance="'. $NationalInsuranceKCS . '"]');

            foreach ($xpQuery as $item)
            {

                $newItem = $schoolExtract->documentElement->appendChild(
                        $schoolExtract->createElement('ContractOrServiceGroup')
                    );

                foreach (array('ContractType', 'ContractStart', 'ContractEnd','Post', 'SchoolArrivalDate', 'DestinationCode', 'Origin', 'LASchoolLevel', 'Estab', 'PostLevelDetails', 'Roles') as $elementName) {
                    $newItem->appendChild(
                        $schoolExtract->importNode(
                            $item->getElementsByTagName($elementName)->item(0),
                            true
                        )
                    );
                }

            }

            $i++;               
        }           

    }

    if($foundRecord == 1){

        /*No Handlers in place yet*/
    }
    else if($foundRecord == 0){
        /*No Handlers in place yet*/

    }

}

$schoolExtract->formatOutput = true;

echo $schoolExtract->saveXml();

?>

schoolExtract.xml

<Members>
    <SchoolWorkforceMembers>
        <SchoolWorkforceMember>
            <StaffDetails>
                <TeacherNumber>123456</TeacherNumber>
                <StaffMemberName>
                  <PersonFamilyName>Wayne</PersonFamilyName>
                  <GivenNames>
                    <GivenName>
                      <PersonGivenName>Bruce</PersonGivenName>
                    </GivenName>
                  </GivenNames>
                </StaffMemberName>
                <NINumber>BW123456</NINumber>
                <GenderCurrent>2</GenderCurrent>
                <PersonBirthDate>1974-08-07</PersonBirthDate>
                <Ethnicity>WBRI</Ethnicity>
                <Disability>NOBT</Disability>
                <QTStatus>true</QTStatus>
                <HLTAStatus>false</HLTAStatus>
          </StaffDetails>
           <Absences>
                <Absence>
                  <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence>
                  <LastDayOfAbsence>2011-10-30</LastDayOfAbsence>
                  <WorkingDaysLost>033.0</WorkingDaysLost>
                  <AbsenceCategory>SIC</AbsenceCategory>
                  <Estab>6905</Estab>
                </Absence>
                <Absence>
                  <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence>
                  <LastDayOfAbsence>2012-01-17</LastDayOfAbsence>
                  <AbsenceCategory>OTH</AbsenceCategory>
                  <Estab>6905</Estab>
                </Absence>
              </Absences>
        </SchoolWorkforceMember> 
        <SchoolWorkforceMember>
            <StaffDetails>
                <TeacherNumber>123456</TeacherNumber>
                <StaffMemberName>
                  <PersonFamilyName>Parker</PersonFamilyName>
                  <GivenNames>
                    <GivenName>
                      <PersonGivenName>Peter</PersonGivenName>
                    </GivenName>
                  </GivenNames>
                </StaffMemberName>
                <NINumber>SM123456</NINumber>
                <GenderCurrent>2</GenderCurrent>
                <PersonBirthDate>1974-08-07</PersonBirthDate>
                <Ethnicity>WBRI</Ethnicity>
                <Disability>NOBT</Disability>
                <QTStatus>true</QTStatus>
                <HLTAStatus>false</HLTAStatus>
          </StaffDetails>
           <Absences>
                <Absence>
                  <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence>
                  <LastDayOfAbsence>2011-10-30</LastDayOfAbsence>
                  <WorkingDaysLost>033.0</WorkingDaysLost>
                  <AbsenceCategory>SIC</AbsenceCategory>
                  <Estab>6905</Estab>
                </Absence>
                <Absence>
                  <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence>
                  <LastDayOfAbsence>2012-01-17</LastDayOfAbsence>
                  <AbsenceCategory>OTH</AbsenceCategory>
                  <Estab>6905</Estab>
                </Absence>
              </Absences>
        </SchoolWorkforceMember>
    </SchoolWorkforceMembers>   
</Members>

kcsFinalExtract.xml

<KCSExtract>
  <StaffRecord>
    <StaffDetails>
      <NationalInsurance>BW123456</NationalInsurance>
      <PersonBirthDate>1974-08-07</PersonBirthDate>
      <StaffMemberName>Wayne</StaffMemberName>
      <PersonGivenName>Bruce</PersonGivenName>
    </StaffDetails>
    <ContractOrServiceGroup>
      <ContractOrService>
        <ContractType>PRM</ContractType>
        <ContractStart>01/09/2003</ContractStart>
        <ContractEnd></ContractEnd>
        <Post>TCH</Post>
        <SchoolArrivalDate>01/12/2007</SchoolArrivalDate>
        <DestinationCode>TU</DestinationCode>
        <Origin>NOTKNW</Origin>
        <LASchoolLevel></LASchoolLevel>
        <Estab></Estab>
        <PostLevelDetails>
          <Payments>
            <Scale>TU</Scale>
            <RegionSpine>EW</RegionSpine>
            <SpinePoint>U2</SpinePoint>
            <TotalPay>35,447.00</TotalPay>
            <SafeguardedSalary>FALSE</SafeguardedSalary>
          </Payments>
          <Hours>
            <Hours>32.5</Hours>
            <FTEHours>32.5</FTEHours>
            <WeeksPerYear>52</WeeksPerYear>
          </Hours>
        </PostLevelDetails>
        <Roles>
          <Role>
            <RoleIdentifier>TCHR</RoleIdentifier>
          </Role>
        </Roles>
      </ContractOrService>
    </ContractOrServiceGroup>
  </StaffRecord>
    <StaffRecord>
    <StaffDetails>
      <NationalInsurance>SM123456</NationalInsurance>
      <PersonBirthDate>03/01/1971</PersonBirthDate>
      <StaffMemberName>Parker</StaffMemberName>
      <PersonGivenName>Peter</PersonGivenName>
    </StaffDetails>
    <ContractOrServiceGroup>
      <ContractOrService>
        <ContractType>PRM</ContractType>
        <ContractStart>01/09/2003</ContractStart>
        <ContractEnd></ContractEnd>
        <Post>TCH</Post>
        <SchoolArrivalDate>01/12/2007</SchoolArrivalDate>
        <DestinationCode>TU</DestinationCode>
        <Origin>NOTKNW</Origin>
        <LASchoolLevel></LASchoolLevel>
        <Estab></Estab>
        <PostLevelDetails>
          <Payments>
            <Scale>TU</Scale>
            <RegionSpine>EW</RegionSpine>
            <SpinePoint>U2</SpinePoint>
            <TotalPay>35,447.00</TotalPay>
            <SafeguardedSalary>FALSE</SafeguardedSalary>
          </Payments>
          <Hours>
            <Hours>32.5</Hours>
            <FTEHours>32.5</FTEHours>
            <WeeksPerYear>52</WeeksPerYear>
          </Hours>
        </PostLevelDetails>
        <Roles>
          <Role>
            <RoleIdentifier>TCHR</RoleIdentifier>
          </Role>
        </Roles>
      </ContractOrService>
    </ContractOrServiceGroup>
  </StaffRecord>
  </KCSExtract>

生成的输出

<?xml version="1.0"?>
<Members>
    <SchoolWorkforceMembers>
        <SchoolWorkforceMember>
            <StaffDetails>
                <TeacherNumber>123456</TeacherNumber>
                <StaffMemberName>
                  <PersonFamilyName>Wayne</PersonFamilyName>
                  <GivenNames>
                    <GivenName>
                      <PersonGivenName>Bruce</PersonGivenName>
                    </GivenName>
                  </GivenNames>
                </StaffMemberName>
                <NINumber>BW123456</NINumber>
                <GenderCurrent>2</GenderCurrent>
                <PersonBirthDate>1974-08-07</PersonBirthDate>
                <Ethnicity>WBRI</Ethnicity>
                <Disability>NOBT</Disability>
                <QTStatus>true</QTStatus>
                <HLTAStatus>false</HLTAStatus>
          </StaffDetails>
           <Absences>
                <Absence>
                  <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence>
                  <LastDayOfAbsence>2011-10-30</LastDayOfAbsence>
                  <WorkingDaysLost>033.0</WorkingDaysLost>
                  <AbsenceCategory>SIC</AbsenceCategory>
                  <Estab>6905</Estab>
                </Absence>
                <Absence>
                  <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence>
                  <LastDayOfAbsence>2012-01-17</LastDayOfAbsence>
                  <AbsenceCategory>OTH</AbsenceCategory>
                  <Estab>6905</Estab>
                </Absence>
              </Absences>
        </SchoolWorkforceMember> 
        <SchoolWorkforceMember>
            <StaffDetails>
                <TeacherNumber>123456</TeacherNumber>
                <StaffMemberName>
                  <PersonFamilyName>Parker</PersonFamilyName>
                  <GivenNames>
                    <GivenName>
                      <PersonGivenName>Peter</PersonGivenName>
                    </GivenName>
                  </GivenNames>
                </StaffMemberName>
                <NINumber>SM123456</NINumber>
                <GenderCurrent>2</GenderCurrent>
                <PersonBirthDate>1974-08-07</PersonBirthDate>
                <Ethnicity>WBRI</Ethnicity>
                <Disability>NOBT</Disability>
                <QTStatus>true</QTStatus>
                <HLTAStatus>false</HLTAStatus>
          </StaffDetails>
           <Absences>
                <Absence>
                  <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence>
                  <LastDayOfAbsence>2011-10-30</LastDayOfAbsence>
                  <WorkingDaysLost>033.0</WorkingDaysLost>
                  <AbsenceCategory>SIC</AbsenceCategory>
                  <Estab>6905</Estab>
                </Absence>
                <Absence>
                  <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence>
                  <LastDayOfAbsence>2012-01-17</LastDayOfAbsence>
                  <AbsenceCategory>OTH</AbsenceCategory>
                  <Estab>6905</Estab>
                </Absence>
              </Absences>
        </SchoolWorkforceMember>
    </SchoolWorkforceMembers>   
<ContractOrServiceGroup><ContractType>PRM</ContractType><ContractStart>01/09/2003</ContractStart><ContractEnd/><Post>TCH</Post><SchoolArrivalDate>01/12/2007</SchoolArrivalDate><DestinationCode>TU</DestinationCode><Origin>NOTKNW</Origin><LASchoolLevel/><Estab/><PostLevelDetails><Payments><Scale>TU</Scale><RegionSpine>EW</RegionSpine><SpinePoint>U2</SpinePoint><TotalPay>35,447.00</TotalPay><SafeguardedSalary>FALSE</SafeguardedSalary></Payments><Hours><Hours>32.5</Hours><FTEHours>32.5</FTEHours><WeeksPerYear>52</WeeksPerYear></Hours></PostLevelDetails><Roles><Role><RoleIdentifier>TCHR</RoleIdentifier></Role></Roles></ContractOrServiceGroup><ContractOrServiceGroup><ContractType>PRM</ContractType><ContractStart>01/09/2003</ContractStart><ContractEnd/><Post>TCH</Post><SchoolArrivalDate>01/12/2007</SchoolArrivalDate><DestinationCode>TU</DestinationCode><Origin>NOTKNW</Origin><LASchoolLevel/><Estab/><PostLevelDetails><Payments><Scale>TU</Scale><RegionSpine>EW</RegionSpine><SpinePoint>U2</SpinePoint><TotalPay>35,447.00</TotalPay><SafeguardedSalary>FALSE</SafeguardedSalary></Payments><Hours><Hours>32.5</Hours><FTEHours>32.5</FTEHours><WeeksPerYear>52</WeeksPerYear></Hours></PostLevelDetails><Roles><Role><RoleIdentifier>TCHR</RoleIdentifier></Role></Roles></ContractOrServiceGroup></Members>

期望的输出

<Members>
  <SchoolWorkforceMembers>
    <SchoolWorkforceMember>
      <StaffDetails>
        <TeacherNumber>123456</TeacherNumber>
        <StaffMemberName>
          <PersonFamilyName>Wayne</PersonFamilyName>
          <GivenNames>
            <GivenName>
              <PersonGivenName>Bruce</PersonGivenName>
            </GivenName>
          </GivenNames>
        </StaffMemberName>
        <NINumber>BW123456</NINumber>
        <GenderCurrent>2</GenderCurrent>
        <PersonBirthDate>1974-08-07</PersonBirthDate>
        <Ethnicity>WBRI</Ethnicity>
        <Disability>NOBT</Disability>
        <QTStatus>true</QTStatus>
        <HLTAStatus>false</HLTAStatus>
      </StaffDetails>
      <ContractOrServiceGroup>
        <ContractOrService>
          <ContractType>PRM</ContractType>
          <ContractStart>01/09/2003</ContractStart>
          <ContractEnd></ContractEnd>
          <Post>TCH</Post>
          <SchoolArrivalDate>01/12/2007</SchoolArrivalDate>
          <DestinationCode>TU</DestinationCode>
          <Origin>NOTKNW</Origin>
          <LASchoolLevel></LASchoolLevel>
          <Estab></Estab>
          <PostLevelDetails>
            <Payments>
              <Scale>TU</Scale>
              <RegionSpine>EW</RegionSpine>
              <SpinePoint>U2</SpinePoint>
              <TotalPay>35,447.00</TotalPay>
              <SafeguardedSalary>FALSE</SafeguardedSalary>
            </Payments>
            <Hours>
              <Hours>32.5</Hours>
              <FTEHours>32.5</FTEHours>
              <WeeksPerYear>52</WeeksPerYear>
            </Hours>
          </PostLevelDetails>
          <Roles>
            <Role>
              <RoleIdentifier>TCHR</RoleIdentifier>
            </Role>
          </Roles>
        </ContractOrService>
      </ContractOrServiceGroup>
      <Absences>
        <Absence>
          <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence>
          <LastDayOfAbsence>2011-10-30</LastDayOfAbsence>
          <WorkingDaysLost>033.0</WorkingDaysLost>
          <AbsenceCategory>SIC</AbsenceCategory>
          <Estab>6905</Estab>
        </Absence>
        <Absence>
          <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence>
          <LastDayOfAbsence>2012-01-17</LastDayOfAbsence>
          <AbsenceCategory>OTH</AbsenceCategory>
          <Estab>6905</Estab>
        </Absence>
      </Absences>
    </SchoolWorkforceMember>
    <SchoolWorkforceMember>
      <StaffDetails>
        <TeacherNumber>123456</TeacherNumber>
        <StaffMemberName>
          <PersonFamilyName>Parker</PersonFamilyName>
          <GivenNames>
            <GivenName>
              <PersonGivenName>Peter</PersonGivenName>
            </GivenName>
          </GivenNames>
        </StaffMemberName>
        <NINumber>SM123456</NINumber>
        <GenderCurrent>2</GenderCurrent>
        <PersonBirthDate>1974-08-07</PersonBirthDate>
        <Ethnicity>WBRI</Ethnicity>
        <Disability>NOBT</Disability>
        <QTStatus>true</QTStatus>
        <HLTAStatus>false</HLTAStatus>
      </StaffDetails>
      <ContractOrServiceGroup>
        <ContractOrService>
          <ContractType>PRM</ContractType>
          <ContractStart>01/09/2003</ContractStart>
          <ContractEnd></ContractEnd>
          <Post>TCH</Post>
          <SchoolArrivalDate>01/12/2007</SchoolArrivalDate>
          <DestinationCode>TU</DestinationCode>
          <Origin>NOTKNW</Origin>
          <LASchoolLevel></LASchoolLevel>
          <Estab></Estab>
          <PostLevelDetails>
            <Payments>
              <Scale>TU</Scale>
              <RegionSpine>EW</RegionSpine>
              <SpinePoint>U2</SpinePoint>
              <TotalPay>35,447.00</TotalPay>
              <SafeguardedSalary>FALSE</SafeguardedSalary>
            </Payments>
            <Hours>
              <Hours>32.5</Hours>
              <FTEHours>32.5</FTEHours>
              <WeeksPerYear>52</WeeksPerYear>
            </Hours>
          </PostLevelDetails>
          <Roles>
            <Role>
              <RoleIdentifier>TCHR</RoleIdentifier>
            </Role>
          </Roles>
        </ContractOrService>
      </ContractOrServiceGroup>
      <Absences>
        <Absence>
          <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence>
          <LastDayOfAbsence>2011-10-30</LastDayOfAbsence>
          <WorkingDaysLost>033.0</WorkingDaysLost>
          <AbsenceCategory>SIC</AbsenceCategory>
          <Estab>6905</Estab>
        </Absence>
        <Absence>
          <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence>
          <LastDayOfAbsence>2012-01-17</LastDayOfAbsence>
          <AbsenceCategory>OTH</AbsenceCategory>
          <Estab>6905</Estab>
        </Absence>
      </Absences>
    </SchoolWorkforceMember>
  </SchoolWorkforceMembers>
</Members>
4

1 回答 1

0

您正在创建一个新的容器元素ContractOrServiceGroup,然后将其附加到文档的根元素,然后在其上附加更多元素。

            $newItem = $schoolExtract->documentElement->appendChild(
                    $schoolExtract->createElement('ContractOrServiceGroup')
                );

相反,您应该做的是创建新元素及其所有子元素,然后将其附加到文档的适当位置。

例如:

           $newItem = $schoolExtract->createElement('ContractOrServiceGroup');

           foreach (array('ContractType', 'etc')) {
               // build $newItem tree
           }

           // $row2 I think is the StaffDetails element in schoolExtract.xml?

           // If so, this will add to the end of StaffDetails's parent:
           $row2->parentNode->appendChild($newItem);

           // This will add to the beginning:
           $row2->parentNode->insertBefore($newItem, $row2->parentNode->firstChild);

           // This will add it before StaffDetails:
           $row2->parentNode->insertBefore($newItem, $row2);

           // This will add it after:
           $row2->parentNode->insertBefore($newItem, $row2->nextSibling);

这段代码很难理解。您是否考虑过改用SimpleXML?对于您正在使用的数据 XML,它是一个更清晰的 API。

于 2012-10-24T00:31:21.983 回答