2017-06-02 20 views
1

我有一個XML文件並且有300個元素。我只想從中取出10條最新記錄並創建另一個XML文件。如何使用PHP中的另一個XML文件創建前50個記錄的XML文件

如果你能給我一些想法,我會非常感激嗎?

PHP

$file = '/directory/xmlfile.xml'; 
if(!$xml = simplexml_load_file($file)){ 
    exit('Failed to open '.$file); 
} else{ 
    print_r($xml); 
    // I want to do some logic here to retrieve top 10 records from file and then create another xml file with 10 records 
} 

XML樣本數據

<data> 
    <total>212</total> 
    <start>0</start> 
    <count>212</count> 
    <data> 
     <item0> 
      <id>123</id> 
      <title>abc-test1</title> 
      <clientContact> 
       <id>111</id> 
       <firstName>abc</firstName> 
       <lastName>xyz</lastName> 
       <email>[email protected]</email> 
      </clientContact> 
      <isOpen>1</isOpen> 
      <isPublic>1</isPublic> 
      <isJobcastPublished>1</isJobcastPublished> 
      <owner> 
       <id>222</id> 
       <firstName>testname</firstName> 
       <lastName>testlastname</lastName> 
       <address> 
        <address1>test address,</address1> 
        <address2>test</address2> 
        <city>City</city> 
        <state>state</state> 
        <zip>2222</zip> 
        <countryID>22</countryID> 
        <countryName>Country</countryName> 
        <countryCode>ABC</countryCode> 
       </address> 
       <email>[email protected]</email> 
       <customText1>test123</customText1> 
       <customText2>testxyz</customText2> 
      </owner> 
      <publicDescription> 
       <p>test info</p> 
      </publicDescription> 
      <status>test</status> 
      <dateLastModified>22222</dateLastModified> 
      <customText4>test1</customText4> 
      <customText10>test123</customText10> 
      <customText11>test</customText11> 
      <customText16>rtest</customText16> 
      <_score>123</_score> 
     </item0> 
     <item1> 
     ... 
     </item1> 
     ... 
    </data> 
</data> 
+0

也許你也可以分享XML的一個片段你想要閱讀以便我們瞭解結構的外觀如何? – codedge

+0

@codedge我已經添加了一個示例XML代碼 – akhan

+0

如何確定日期?它是由位置:前十或最後十個''元素? – Parfait

回答

1

考慮XSLT,旨在變換/操縱XML的各種最終用途,例如提取十大<item*>標籤的專用語言。不需要foreachif邏輯。 PHP維護一個可以在.ini文件中啓用的XSLT處理器(php-xsl)

具體來說,XSLT運行Identity Transform複製文檔,然後寫了一個空白模板項目與位置節點超過10 XML是有點困難的,因爲相同的父/子<data>標籤。

XSLT(保存爲文件名爲.xsl這是一個良好的XML)

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="*[substring(name(),1,4)='item' and position() &gt; 10]"/> 

</xsl:stylesheet> 

PHP

$file = '/directory/xmlfile.xml'; 

if(!$xml = simplexml_load_file($file)) { 
    exit('Failed to open '.$file);  
} else { 
    // Load XSLT  
    $xsl = new DOMDocument; 
    $xsl->load('/path/to/xsl_script.xsl'); 

    // Configure transformer 
    $proc = new XSLTProcessor; 
    $proc->importStyleSheet($xsl); 

    // Transform XML source 
    $newXML = new DOMDocument; 
    $newXML = $proc->transformToXML($xml); 

    // Echo new XML tree 
    echo $newXML; 

    // Save output to file 
    $xmlfile = '/path/to/output.xml'; 
    file_put_contents($xmlfile, $newXML);  
} 
相關問題