Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 替换xml远程文件中的值并保存_Php_Xml_Replace_Find_Preg Replace - Fatal编程技术网

Php 替换xml远程文件中的值并保存

Php 替换xml远程文件中的值并保存,php,xml,replace,find,preg-replace,Php,Xml,Replace,Find,Preg Replace,我有一个远程xml文件: 此文件具有以下值: <files> <file> <unique>444444</unique> </file> <file> <unique>666666</unique> </file> <file> <unique>888888</unique> </file&

我有一个远程xml文件: 此文件具有以下值:

<files>

  <file>
     <unique>444444</unique>
  </file>
  <file>
     <unique>666666</unique>
  </file>
  <file>
     <unique>888888</unique>
  </file>

</files>

您可以对模式使用preg_replace_回调:

$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);
$txt=asXml('new-xml.xml')//创建xml
//打印(newXml);
xpath('//unique');
foreach($设置为&$项)
$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>

222222
333333
444444
使用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>


这是不正确的关闭标记-
simplexml无法与it@splash58是一个输入错误,我刚刚编辑了我的文章。仅针对唯一元素?@miglio是的,只是文件名无效,示例在到达行时停止对加载操作的返回值进行操作。很抱歉,它不起作用。你能在我的代码的xml函数中插入你的代码吗?我想我在那里迷路了。如果不工作,您可以插入此代码以以下两行开始您的代码:“$xml_external_path=”;”和“$xml=simplexml_load_file($xml_external_path);”看起来不错,但是如何将新文件保存为'new-xml.xml'asxml,文件名为参数$xml->asxml('new-xml.xml');您能用这两行代码开头吗:“$xml_external_path=”;”和“$xml=simplexml_load_file($xml_external_path);”只把我的第一行改成你的两行。一切都会成功的
<?xml version="1.0"?>
<files>
  <file>
     <unique>222222</unique>
  </file>
  <file>
     <unique>333333</unique>
  </file>
  <file>
     <unique>444444</unique>
  </file>
</files>
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);
<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>