需要在PHP中按属性值删除xml子项

时间:2017-07-22 12:23:31

标签: php xml xml-parsing

我想从这个xml中删除子标记。例如,我想删除这个

<div class="container">
   <!--?xml version="1.0" encoding="UTF-8"?-->
   <svg viewBox="0 0 100 100">
     <g class="shape" transform="scale(0.1) translate(-300, 0) rotate(10)">
       <polygon xmlns="http://www.w3.org/2000/svg" points="350,75  379,161 469,161 397,215                     423,301 350,250 277,301 303,215                     231,161 321,161" />
     </g>
   </svg>
</div>

子。我正在以这种方式解析xml。

interact('.shape').draggable({
  onstart: dragStartListener,
  onmove: dragMoveListener,
  onend: dragEndListener
});

var svg = document.getElementsByTagName('svg')[0];
var pt = svg.createSVGPoint();

function dragStartListener(event) {
  event.target.transform.baseVal.consolidate();
  console.clear();
}

function dragMoveListener(event) {
  var target = event.target;
  var currentCursor = getCurrentCursor(event);
  var translationMatrix = getMatrixForTranslation(target, currentCursor.x, currentCursor.y);
  target.transform.baseVal.getItem(0).setMatrix(target.transform.baseVal.getItem(0).matrix.multiply(translationMatrix));
}

function dragEndListener(event) {
  var target = event.target;
  target.removeAttribute('data-x');
  target.removeAttribute('data-y');
}

function getMatrixForTranslation(target, x, y) {
  return target.ownerSVGElement.createSVGMatrix().translate(x, y);
}

function getCurrentCursor(event) {
  var target = event.target;
  // keep the change in cursor on page in the data-x/data-y attributes
  var x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
  var y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
  target.setAttribute('data-x', x);
  target.setAttribute('data-y', y);
  pt.x = event.x0 + x; // calculate the new whole page coordinates
  pt.y = event.y0 + y;
  // convert to object space coordinates  ...  https://stackoverflow.com/a/5223921/5610106
  var globalPoint = pt.matrixTransform(svg.getScreenCTM().inverse());
  var globalToLocal = target.getTransformToElement(svg).inverse();
  var inObjectSpace = globalPoint.matrixTransform(globalToLocal);
  console.log('cursor: x: ' + inObjectSpace.x + '; y: ' + inObjectSpace.y);
  return {x: inObjectSpace.x, y: inObjectSpace.y};
}

但未能删除。我已经完成了stackoverflow上的所有anwsers,但未能得到解决方案。任何帮助将不胜感激。我的xml如下:

"<ExtraCategory diffgr:id="ExtraCategory1" msdata:rowOrder="0">
        <Barcode>5051694676090</Barcode>
        <CategoryID>1</CategoryID>
        <Value>High Quality</Value>
</ExtraCategory>"

1 个答案:

答案 0 :(得分:1)

您不能只是unset() XML节点,您需要从DOM中删除该节点。有点像...

   $Xmlresult = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<DocumentElement
    xmlns:diffgr="http://someURL" xmlns:msdata="http://someURL">
    <ExtraCategory diffgr:id="ExtraCategory1" msdata:rowOrder="0">
        <Barcode>5051694676090</Barcode>
        <CategoryID>1</CategoryID>
        <Value>High Quality</Value>
    </ExtraCategory>
    <ExtraCategory diffgr:id="ExtraCategory2" msdata:rowOrder="1">
        <Barcode>5051694676090</Barcode>
        <CategoryID>2</CategoryID>
        <Value>Stylish Appearance</Value>
    </ExtraCategory>
    <ExtraCategory diffgr:id="ExtraCategory3" msdata:rowOrder="2">
        <Barcode>5051694676090</Barcode>
        <CategoryID>3</CategoryID>
        <Value>In Fashion</Value>
    </ExtraCategory>
</DocumentElement>
XML;

$doc = new DOMDocument();
$doc->loadXML($Xmlresult);
$xpath = new DOMXpath($doc);
$id="ExtraCategory1";
$xpath->registerNamespace("diffgr", "http://someURL");
$removeNode = $xpath->query("//ExtraCategory[@diffgr:id=\"$id\"]")[0];
$removeNode->parentNode->removeChild($removeNode);
echo $doc->saveXML();

需要registerNamespace来允许您在XPath表达式中引用命名空间,您需要更正命名空间,因为这只是我用来使其工作的虚拟命名空间。

主要位是使用removeChild,这是一个复杂的语句,但它需要删除的节点,上升到它的父节点,然后从中删除子节点。

会给......

<?xml version="1.0" encoding="UTF-8"?>
<DocumentElement xmlns:diffgr="http://someURL" xmlns:msdata="http://someURL">

    <ExtraCategory diffgr:id="ExtraCategory2" msdata:rowOrder="1">
        <Barcode>5051694676090</Barcode>
        <CategoryID>2</CategoryID>
        <Value>Stylish Appearance</Value>
    </ExtraCategory>
    <ExtraCategory diffgr:id="ExtraCategory3" msdata:rowOrder="2">
        <Barcode>5051694676090</Barcode>
        <CategoryID>3</CategoryID>
        <Value>In Fashion</Value>
    </ExtraCategory>
</DocumentElement>

(注意我添加了命名空间的声明,你可能有自己的定义,但我需要添加一些东西。)