2013-03-01 33 views
1

這是我的XML O/P: -的XML更改爲類似樹明智

<products> 
    <region_timezone>1</region_timezone> 
    <registrationstatus>2</registrationstatus> 
    <eventstatus>2</eventstatus> 
    <dist_activity>5,10068,10070</dist_activity> 
    <dist_region>5069,5069,5069</dist_region> 
    <dist_value>55,342,86</dist_value> 
    <dist_unit>1,1,1</dist_unit> 
    <dist_map>5</dist_map> 
    <entryfee_currency>USD</entryfee_currency> 
    <reg_str_dt>2013-01-14 20:35:00</reg_str_dt> 
    <reg_end_dt>2013-01-14 20:35:00</reg_end_dt> 
    <individual_label>19+++</individual_label> 
    <individual_born_from>1980-08-21</individual_born_from> 
    <individual_born_to>2010-08-18</individual_born_to> 
    <individual_sex>3</individual_sex> 
    <individual_strdt>2013-01-14 20:35:00</individual_strdt> 
    <individual_start>2013-01-14 20:35:00</individual_start> 
    <elite_sex>1</elite_sex> 
    <tab_id>351</tab_id> 
    <product_id>1</product_id> 
    <tab_name>test1</tab_name> 
    <region_timezone>1</region_timezone> 
    <registrationstatus>2</registrationstatus> 
    <eventstatus>2</eventstatus> 
    <dist_activity>5,10069,10070</dist_activity> 
    <dist_region>4457,7140,5069</dist_region> 
    <dist_value>55,213,86</dist_value> 
    <dist_unit>1,1,1</dist_unit> 
    <dist_map>5</dist_map> 
    <entryfee_currency>USD</entryfee_currency> 
    <reg_str_dt>2013-02-14 20:39:00</reg_str_dt> 
    <reg_end_dt>2013-02-14 20:39:00</reg_end_dt> 
    <individual_label>19+++</individual_label> 
    <individual_born_from>1980-08-21</individual_born_from> 
    <individual_born_to>2010-08-18</individual_born_to> 
    <individual_sex>3</individual_sex> 
    <individual_strdt>2013-02-14 20:39:00</individual_strdt> 
    <individual_start>2013-02-14 20:39:00</individual_start> 
    <elite_sex>1</elite_sex> 
    <tab_id>352</tab_id> 
    <product_id>2</product_id> 
    <tab_name>test2</tab_name> 
</products> 

這裏是我的代碼(test.xsl): -

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:strip-space elements="*"/> 

<xsl:template match="*"> 
    <xsl:copy> 
    <xsl:apply-templates select="*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="node"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="/"> 
    <xsl:apply-templates /> 
</xsl:template> 

<!-- from dimitre\'s xsl.thanks --> 
<xsl:template match="node[position()>1]/text()"> 
    <xsl:text>,</xsl:text> 
    <xsl:value-of select="."/> 
</xsl:template> 
</xsl:stylesheet> 

這是我的PHP文件: -

$sourcedoc = new DOMDocument(); 
$sourcedoc->load('test.xml'); 

$stylesheet = new DOMDocument(); 
$stylesheet->load('test.xsl'); 

// create a new XSLT processor and load the stylesheet 
$xsltprocessor = new XSLTProcessor(); 
$xsltprocessor->importStylesheet($stylesheet); 

// save the new xml file 
file_put_contents('test-translated.xml', $xsltprocessor->transformToXML($sourcedoc)); 

我想這樣的一些變化,其中: -

<product> 
    <product_id value="1"> 
    <tab_id value="351"> 
     <tab_name value="test1"></tab_name> 
     <region_id>1</region_id> 
     <region_time>27,02,2013</region_time> 
    </tab_id> 
    </product_id> 
</product> 

我想這種類型的輸出...

,如果可能使用XSLT那麼好...

其他明智的任何方式來解決他們幫我
感謝..
準確我有多個標籤,如果我申請對他們這段代碼有沒有產生完美的O/...因爲我需要

回答

0

這XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

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

    <xsl:template match="product"> 
    <xsl:copy> 
     <xsl:apply-templates select="product_id" /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="product_id"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*" /> 
     <xsl:apply-templates select="../tab_id" /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="tab_id"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*"/> 
     <xsl:apply-templates select="../tab_name" /> 
     <xsl:apply-templates select="../region_id | ../region_time" /> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

當你的樣品輸入運行,產生這樣的輸出:

<product> 
    <product_id value="1"> 
    <tab_id value="351"> 
     <tab_name value="test1" /> 
     <region_id>1</region_id> 
     <region_time>27,02,2013</region_time> 
    </tab_id> 
    </product_id> 
</product> 

是你要去的事情,或者是你尋找的東西更通用的?如果你需要更通用的東西,請解釋你的場景如何變得更一般?

+0

感謝JLRishe其良好的工作,但我有多個選項卡,所以我應用此代碼,因此它不會生成完美的o/p ..請檢查我的xml編輯 – user2122438 2013-03-02 07:13:42

+0

您的示例XML現在沒有region_ids或region_times。那些從哪裏來? – JLRishe 2013-03-02 08:23:57

+0

它只是一個示例xml和我們的xsl文件我改變了這一行 user2122438 2013-03-02 09:12:16