1

这是我的 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 / ...我需要

4

1 回答 1

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>

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

于 2013-03-01T13:49:07.990 回答