将xml更改为类似于树的形式
这是我的xml o/p:-将xml更改为类似于树的形式,xml,xslt,Xml,Xslt,这是我的xml o/p:- <products> <region_timezone>1</region_timezone> <registrationstatus>2</registrationstatus> <eventstatus>2</eventstatus> <dist_activity>5,10068,10070</dist_activity>
<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>
我想对它们进行如下更改:-
<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>
1.
27,02,2013
我想要这种类型的输出…如果可以使用Xslt,那么很好…
其他解决方法请帮助我
谢谢..
确切地说,我有多个标签,如果我在他们身上应用此代码没有生成完美的o/。。。如我所需,此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>
1.
27,02,2013
这就是你想要的东西,还是你在寻找更普通的东西?如果您需要更通用的内容,请解释如何使您的场景更通用?谢谢JLRishe它的工作很好,但我有多个选项卡,因此我将此代码应用于它们,这样就不会生成完美的o/p。。请检查我在XML中编辑的内容您的示例XML现在没有区域ID或区域时间。这些应该从哪里来?这只是一个示例xml,使用我们的xsl文件,我正在更改这一行,我不知道这意味着什么。在我帮助您之前,您需要明确定义您的需求。当前示例XML的期望输出是什么?请检查以下问题:-
<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>