将xml更改为类似树的方式

时间:2013-03-01 12:43:11

标签: xml xslt

这是我的xml o / p: -

<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 / ...因为我需要

1 个答案:

答案 0 :(得分: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>

这是你想要的那种东西,还是你在寻找更通用的东西?如果您需要更通用的内容,请解释如何使您的方案更加通用?