I have the below php to;
- concatenate and ";" separate the similar XML child tags
- Output a XML file
code
<?php
$txt = '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<Results productTitle="abc DB/TextWorks" productVersion="1.00">
<Recordset setCount="3">
<Record setEntry="0">
<TI>TEST &</TI>
<AU>One &</AU>
<AU>Two &</AU>
<AU>three</AU>
</Record>
<Record setEntry="1">
<AU>One &</AU>
<AU>Two</AU>
<AU>Three</AU>
<AU>Four</AU>
</Record>
<Record setEntry="2">
<AUA>One &</AUA>
<AU>2</AU>
<AU></AU>
<AU></AU>
</Record>
</Recordset>
</Results>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($txt);
$xp = new DOMXPath($dom);
$cnode_type = array();
foreach ($xp->query("/Results/Recordset/Record/*") as $c) {
$cnode_type[] = $c->nodeName;
}
$cnode_type = array_unique($cnode_type);
$recs = $xp->query("/Results/Recordset/Record");
foreach ($recs as $par) {
foreach ($cnode_type as $c) {
if ($xp->evaluate("count($c)", $par) > 1) {
$node_vals = [];
foreach ($xp->query($c, $par) as $n) {
if (isset($n->nodeValue) && strlen($n->nodeValue) > 0) {
$node_vals[] = $n->nodeValue;
}
$par->removeChild($n);
}
$new_node = $dom->createElement($c, implode("; ", $node_vals));
$par->appendChild($new_node);
}
}
}
echo $dom->saveXML();
I need few more thing to get done. 1. Replace "&" with "&" in the XML code 2. Sort XML child tags in the ascending order
No comments:
Post a Comment