2013-03-26 61 views
0

這是我的鏈接,以獲得XML文件: - XML LINK從XML文件中刪除Name空間,並保存爲新的XML

這是我的代碼: -

<?php 
function convertNodeValueChars($node) { 
    if ($node->hasChildNodes()) { 
     foreach ($node->childNodes as $childNode) { 
     if ($childNode->nodeType == XML_TEXT_NODE) { 
      $childNode->nodeValue = iconv('utf-8', 'ascii//TRANSLIT', $childNode->nodeValue); 
     } 
     convertNodeValueChars($childNode);   
     } 
    }  
    } 

    $doc = new DOMDocument(); 
    $doc->load('http://services.gisgraphy.com/geoloc/search?lat=13o6&lng=80o12&radius=7000'); 
    convertNodeValueChars($doc->documentElement); 
    $doc->save('general.xml'); 
?> 

1)本人試圖刪除ASCII字符爲普通字符
2)要刪除XML文件,這是的名稱 - 空間包含名稱空間<results xmlns="http://gisgraphy.com">
3)要保存爲另一個XML文件

+1

這個答案有你需要的東西:http://stackoverflow.com/a/10736557/18771 – Tomalak 2013-03-26 10:10:13

+0

@Tomalak沒有這不是有用的。我想刪除使用PHP – 2013-03-26 10:39:23

+0

PHP中有XSLT支持。它會花費你10行代碼的順序來使它工作。 – Tomalak 2013-03-26 10:54:17

回答

1

簡單的PHP文件第一次創建加載XML從URL: -

<?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)); 
?>