Accessing a specific node which is also used in other areas in the file



If I have the following data in my XML file;



<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" xmlns:xsi="http://ift.tt/ra1lAU">
<CstmrDrctDbtInitn>
<PmtInf>
<PmtInfId>5n7gfUaPGK</PmtInfId>
<PmtMtd>DD</PmtMtd>
<NbOfTxs>1</NbOfTxs>
<CtrlSum>200.2</CtrlSum>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
<LclInstrm>
<Cd>CORE</Cd>
</LclInstrm>
<SeqTp>RCUR</SeqTp>
</PmtTpInf>
<DrctDbtTxInf>
<PmtId>
<EndToEndId>nmu5AOhE7G</EndToEndId>
</PmtId>
</DrctDbtTxInf>
</PmtInf>
<PmtInf>
<PmtInfId>5jAcoNoId3</PmtInfId>
<PmtMtd>DD</PmtMtd>
<NbOfTxs>3</NbOfTxs>
<CtrlSum>100.5</CtrlSum>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
<LclInstrm>
<Cd>CORE</Cd>
</LclInstrm>
<SeqTp>FRST</SeqTp>
</PmtTpInf>
<DrctDbtTxInf>
<PmtId>
<EndToEndId>nmu5AbdfG</EndToEndId>
</PmtId>
</DrctDbtTxInf>
<DrctDbtTxInf>
<PmtId>
<EndToEndId>nmu5A3r5jgG</EndToEndId>
</PmtId>
</DrctDbtTxInf>
</PmtInf>
</CstmrDrctDbtInitn>
</Document>


How would I access <NbOfTxs> in the second <PmtInf> block (where the value is 3) instead of <NbOfTxs> in the first <PmtInf> block (where the value is 1)?


If I just used the following line of code;



$FRSTTransaction = $xml->getElementsByTagName('NbOfTxs')->nodeValue;


It doesn't know which <NbOfTxs> I am attempting to access.


The only difference between each payment block is the <SeqTp>. There will be 4 Payment Blocks in total.


I am trying to count the number of <DrctDbtTxInf>blocks in each Payment Block and then put this value into <NbOfTxs>.



<PmtId>
<EndToEndId>nmu5AOhE7G</EndToEndId>
</PmtId>
</DrctDbtTxInf>


The code I tried is as follows;



$filename = date('Y-W').'.xml'; //2014-26.xml
$xml = new DOMDocument;
$xml->load($filename);
$NumberTransactions = 0;
$RCURTransaction = $xml->getElementsByTagName('DrctDbtTxInf');
$NodeValue = $xml->getElementsByTagName('NbOfTxs')->nodeValue;
foreach ($RCURTransaction as $Transaction) {
$NodeValue = $NodeValue + 1;
}

$Document = simplexml_load_file($filename);
$Document->CstmrDrctDbtInitn->PmtInf->NbOfTxs = $NodeValue;
$Document->asXML($filename);


I receive no errors, it just doesnt seem to access the node value.


1 comment:

  1. okay, Thanks you for providing us all the information through your blog.


    ISO consultancy services india

    ReplyDelete