Question

This is what I have so far:

This is the output I am receiving: http://i.imgur.com/mGRAJPe.gif

It seems all of the subfield nodes are becoming a child of the 1st datafield element, instead of being nested in their respective datafield parent. Any suggestions?

$str = '<?xml version="1.0" encoding="UTF-8"?><collection     xmlns="http://www.loc.gov/MARC21/slim"><record></record></collection>';
$xml = simplexml_load_string($str);
$xml->record = "";
$xml->record->addChild('leader', $data["controlfield"]["000"]["40"]);

foreach ($data["controlfield"] as $dcf => $value) {
    foreach ($value as $dcfv) {
    $addControl = $xml->record->addChild('controlfield', $dcfv);
    $addControl->addAttribute('tag', $dcf);
    }
}

foreach ($data["datafield"] as $dftc => $dfarray) {
    $addData = $xml->record->addChild('datafield');
    $addData->addAttribute('tag', $dftc);
    $addData->addAttribute('ind1', $dfarray['ind1']);
    $addData->addAttribute('ind2', $dfarray['ind2']);

    foreach ($dfarray as $code => $value) {                         
        if(($code != "ind1") && ($code != "ind2")) {
        $addSubs = $xml->record->datafield->addChild('subfield', $value);
        $addSubs->addAttribute('code', $code);
        }
    }
}
Was it helpful?

Solution

Well, that's what you do here, you handle datafield as a single node, so the first one is used (why should it be the last one?):

$xml->record->datafield->addChild('subfield', $value);

But you already have your desired parent in $addData, just use it:

$addData->addChild('subfield', $value);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top