亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 PHP 刪除 XML 文件的元素

使用 PHP 刪除 XML 文件的元素

PHP
慕后森 2023-07-08 16:25:41
我需要使用 PHP 刪除 XML 文件的元素。這是我的 XML 文件<?xml version="1.0" encoding="utf-8"?><soap:Envelope    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/2001/XMLSchema">    <soap:Body>        <GetVehiculesLocationResponse xmlns="http://google.fr/">            <GetVehiculesLocationResult>                <Location>                    <idVH>001</idVH>                    <date>2020-06-30T09:06:39</date>                    <latitude>111111</latitude>                    <longitude>11111</longitude>                </Location>                <Location>                    <idVH>002</idVH>                    <date>2020-04-02T13:45:51</date>                    <latitude>1111111</latitude>                    <longitude>111111</longitude>                </Location>                <Location>                    <idVH>11111111</idVH>                    <date>2020-03-24T21:49:46</date>                    <latitude>1111111</latitude>                    <longitude>11111111</longitude>                </Location>            </GetVehiculesLocationResult>        </GetVehiculesLocationResponse>    </soap:Body></soap:Envelope>我想刪除元素(在本例中Location),其中idVH是某個值(在本例中為 002)我已經嘗試過,但它不起作用$xml1 = simplexml_load_string($result); $items = $xml1->xpath("/soap:Envelope/soap:Body/GetVehiculesLocationResponse/GetVehiculesLocationResult/Location[idVH = 002]");foreach ($items as $i) unset($i[0]);   echo $xml1->asXML();
查看完整描述

2 回答

?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

問題是該元素GetVehiculesLocationResponse定義了一個新的默認命名空間,因此子元素都在該新命名空間中......


<GetVehiculesLocationResponse xmlns="http://google.fr/">

因此,首先注冊新的名稱空間,然后將其用作較低級別元素中的前綴......


$xml1->registerXPathNamespace("d", "http://google.fr/");

$items = $xml1->xpath("/soap:Envelope/soap:Body/d:GetVehiculesLocationResponse/d:GetVehiculesLocationResult/d:Location[d:idVH = '002']");



查看完整回答
反對 回復 2023-07-08
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

考慮XSLT(XPath 的同級語言),它是一種專用語言,旨在轉換 XML 文件,特別適合處理許多元素。事實上,您甚至可以將參數001從 PHP 傳遞到 XSLT。PHP 可以使用庫的xsl類運行 XSLT 1.0 腳本DOMDocument

XSLT


<?xml version="1.0" ?>?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? xmlns:googl="http://google.fr/">??

? ?<xsl:output method="xml" indent="yes"/>

? ?<xsl:strip-space elements="*"/>


? ?<!-- DEFINE PARAM WITH DEFAULT -->

? ?<xsl:param name="id_param">001</xsl:param>


? ? <!-- IDENTITY TRANSFORM -->

? ? <xsl:template match="@* | node()">

? ? ? ? <xsl:copy>

? ? ? ? ? ? <xsl:apply-templates select="@* | node()"/>

? ? ? ? </xsl:copy>

? ? </xsl:template>


? ? <!-- KEEP NODES BY PARAM VALUE -->

? ? <xsl:template match="googl:GetVehiculesLocationResult">

? ? ? ? <xsl:copy>

? ? ? ? ? ? <xsl:copy-of select="googl:Location[googl:idVH != $id_param]"/>

? ? ? ? </xsl:copy>

? ? </xsl:template>


</xsl:stylesheet>

PHP (在循環中將參數傳遞給 XSLT)


// LOAD XML

$xml = new DOMDocument('1.0', 'UTF-8');

$xml->load($data->xmlFile);


// LOAD XSLT?

$xsl = new DOMDocument('1.0', 'UTF-8');? ?

$xsl->load('XSLT_Script.xsl');


// INITIALIZE TRANSFORMER

$proc = new XSLTProcessor;

$proc->importStyleSheet($xsl);


foreach($param as array('001', '002')) {?

? ? // SET PARAMETER VALUE

? ? $proc->setParameter('', 'id_param', $param);


? ? // TRANSFORM SOURCE

? ? $xml = $proc->transformToDoc($xml);

}


// ECHO TO SCREEN

echo $xml->saveXML();


// SAVE TO FILE

file_put_contents($data->xmlFile, $xml);

輸出


<?xml version="1.0" encoding="UTF-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

? ?<soap:Body>

? ? ? <GetVehiculesLocationResponse xmlns="http://google.fr/">

? ? ? ? ?<GetVehiculesLocationResult>

? ? ? ? ? ? <Location>

? ? ? ? ? ? ? ?<idVH>11111111</idVH>

? ? ? ? ? ? ? ?<date>2020-03-24T21:49:46</date>

? ? ? ? ? ? ? ?<latitude>1111111</latitude>

? ? ? ? ? ? ? ?<longitude>11111111</longitude>

? ? ? ? ? ? </Location>

? ? ? ? ?</GetVehiculesLocationResult>

? ? ? </GetVehiculesLocationResponse>

? ?</soap:Body>

</soap:Envelope>


查看完整回答
反對 回復 2023-07-08
  • 2 回答
  • 0 關注
  • 200 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號