替换 xml 远程文件中的值并保存
Replace values in xml remote file and save
我有一个远程 xml 文件:http://my-site/my-file.xml
此文件具有以下值:
<files>
<file>
<unique>444444</unique>
</file>
<file>
<unique>666666</unique>
</file>
<file>
<unique>888888</unique>
</file>
</files>
我需要用php将<unique>xxxxxx</unique>
的值替换成它的一半,这样文件就应该变成
<files>
<file>
<unique>222222</unique>
</file>
<file>
<unique>333333</unique>
</file>
<file>
<unique>444444</unique>
</file>
</files>
我得到了打开和保存文件的部分功能,但没有查找和替换代码:
$xml_external_path = 'http://my-site/my-file.xml'; // THIS LINE MUST EXIST
$xml = simplexml_load_file($xml_external_path);// THIS LINE MUST EXIST
$searches = array();
$replacements = array();
foreach (....) {
$searches[] = ....
$replacements[] = ....
}
$newXml = simplexml_load_string( str_replace( $searches, $replacements, $xml->asXml() ) );
$newXml->asXml('new-xml.xml');// THIS LINE MUST EXIST
您可以使用 preg_replace_callback 作为模式:
$txt = <<<XML
<?xml version='1.0'?>
<files>
<file>
<unique>444444</unique>
</file>
<file>
<unique>666666</unique>
</file>
<file>
<unique>888888</unique>
</file>
</files>
XML;
// load the xml like a text
$xml_external_path = 'http://my-site/my-file.xml;';
$txt = file_get_contents($xml_external_path);
$pattern = '/<unique>(.*?)<\/unique>/';
$response = preg_replace_callback($pattern,function($match){
$value = intval(trim($match[1]))/2;
return '<unique>'.$value.'</unique>';
},$xml);
$newXml = simplexml_load_string( $response );//
$newXml->asXml('new-xml.xml');//create the xml
//print_r($newXml);
<?
$str = '<files>
<file>
<unique>444444</unique>
</file>
<file>
<unique>666666</unique>
</file>
<file>
<unique>888888</unique>
</file>
</files>';
$xml = simplexml_load_string($str);
$set = $xml->xpath('//unique');
foreach ($set as &$item)
$item[0] = $item/2;
echo $xml->asXml();
结果
<?xml version="1.0"?>
<files>
<file>
<unique>222222</unique>
</file>
<file>
<unique>333333</unique>
</file>
<file>
<unique>444444</unique>
</file>
</files>
xslt 的一种方式:
define('XML_PATH', 'http://my-site/my-file.xml');
define('XSL_PATH', 'divide.xsl');
$xml = new DOMDocument;
$xml->load(XML_PATH);
$xsl = new DOMDocument;
$xsl->load(XSL_PATH);
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
其中 divide.xsl
是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="unique">
<xsl:value-of select=". div 2"/>
</xsl:template>
</xsl:stylesheet>
我有一个远程 xml 文件:http://my-site/my-file.xml 此文件具有以下值:
<files>
<file>
<unique>444444</unique>
</file>
<file>
<unique>666666</unique>
</file>
<file>
<unique>888888</unique>
</file>
</files>
我需要用php将<unique>xxxxxx</unique>
的值替换成它的一半,这样文件就应该变成
<files>
<file>
<unique>222222</unique>
</file>
<file>
<unique>333333</unique>
</file>
<file>
<unique>444444</unique>
</file>
</files>
我得到了打开和保存文件的部分功能,但没有查找和替换代码:
$xml_external_path = 'http://my-site/my-file.xml'; // THIS LINE MUST EXIST
$xml = simplexml_load_file($xml_external_path);// THIS LINE MUST EXIST
$searches = array();
$replacements = array();
foreach (....) {
$searches[] = ....
$replacements[] = ....
}
$newXml = simplexml_load_string( str_replace( $searches, $replacements, $xml->asXml() ) );
$newXml->asXml('new-xml.xml');// THIS LINE MUST EXIST
您可以使用 preg_replace_callback 作为模式:
$txt = <<<XML
<?xml version='1.0'?>
<files>
<file>
<unique>444444</unique>
</file>
<file>
<unique>666666</unique>
</file>
<file>
<unique>888888</unique>
</file>
</files>
XML;
// load the xml like a text
$xml_external_path = 'http://my-site/my-file.xml;';
$txt = file_get_contents($xml_external_path);
$pattern = '/<unique>(.*?)<\/unique>/';
$response = preg_replace_callback($pattern,function($match){
$value = intval(trim($match[1]))/2;
return '<unique>'.$value.'</unique>';
},$xml);
$newXml = simplexml_load_string( $response );//
$newXml->asXml('new-xml.xml');//create the xml
//print_r($newXml);
<?
$str = '<files>
<file>
<unique>444444</unique>
</file>
<file>
<unique>666666</unique>
</file>
<file>
<unique>888888</unique>
</file>
</files>';
$xml = simplexml_load_string($str);
$set = $xml->xpath('//unique');
foreach ($set as &$item)
$item[0] = $item/2;
echo $xml->asXml();
结果
<?xml version="1.0"?>
<files>
<file>
<unique>222222</unique>
</file>
<file>
<unique>333333</unique>
</file>
<file>
<unique>444444</unique>
</file>
</files>
xslt 的一种方式:
define('XML_PATH', 'http://my-site/my-file.xml');
define('XSL_PATH', 'divide.xsl');
$xml = new DOMDocument;
$xml->load(XML_PATH);
$xsl = new DOMDocument;
$xsl->load(XSL_PATH);
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
其中 divide.xsl
是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="unique">
<xsl:value-of select=". div 2"/>
</xsl:template>
</xsl:stylesheet>