首先在简单的 php 文件上创建以从 URL 加载 xml:-
<?php
$dom = new DOMDocument();
$dom->load('http://services.gisgraphy.com/geoloc/search?lat=22.298569900000000000&lng=70.794301799999970000&radius=7000', true);
$dom->save('filename.xml');
?>
然后创建一个 XSLT 文件:-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" version="1.0" encoding="UTF-8" />
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
并创建一个 php 文件来加载 xml 文件并实现我们的 xslt 文件:-
<?php
$sourcedoc->load('filename.xml');
$stylesheet = new DOMDocument();
$stylesheet->load('new4convert.xsl');
// create a new XSLT processor and load the stylesheet
$xsltprocessor = new XSLTProcessor();
$xsltprocessor->importStylesheet($stylesheet);
// save the new xml file
file_put_contents('filename.xml', $xsltprocessor->transformToXML($sourcedoc));
?>
如果您想在一个 PHP 文件中使用最终总代码:-
<?php
$dom = new DOMDocument();
$dom->load('http://services.gisgraphy.com/geoloc/search?lat=22.298569900000000000&lng=70.794301799999970000&radius=7000', true);
$dom->save('filename.xml');
$sourcedoc = new DOMDocument();
$sourcedoc->load('filename.xml');
$stylesheet = new DOMDocument();
$stylesheet->load('new4convert.xsl');
// create a new XSLT processor and load the stylesheet
$xsltprocessor = new XSLTProcessor();
$xsltprocessor->importStylesheet($stylesheet);
// save the new xml file
file_put_contents('filename.xml', $xsltprocessor->transformToXML($sourcedoc));
?>