我有以下xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<group>
<List>
<id_num>1</id_num>
<car>cars</car>
<value>
<color>green</color>
<name>nissan</name>
</value>
<value>
<color>yellow</color>
<name>renault</name>
</value>
<value>
<color>black</color>
<name>ford</name>
</value>
<value_number>3</value_number>
</List>
<List>
<id_num>2</id_num>
<car>cars</car>
<value>
<color>blu</color>
<name>porsche</name>
</value>
<value>
<color>white</color>
<name>volvo</name>
</value>
<value_number>2</value_number>
</List>
</group>
使用xslt我想在节点中获得子节点的不同内容(颜色和名称)&#34; value&#34;即使它具有相同的名称已复制
所需的输出将是此xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<group>group1</group>
<List>
<cars_1>
<car>
<color_info>green</color_info>
<brand_name>nissan</brand_name>
</car>
<car>
<color_info>yellow</color_info>
<brand_name>renault</brand_name>
</car>
<car>
<color_info>black</color_info>
<brand_name>ford</brand_name>
</car>
</cars_1>
<cars_2>
<car>
<color_info>blu</color_info>
<brand_name>porsche</brand_name>
</car>
<car>
<color_info>white</color_info>
<brand_name>volvo</brand_name>
</car>
</cars_2>
</List>
当我尝试使用时 我无法区分具有不同内容的不同节点,因此他们的内容被复制
这是我得到的输出
<?xml version="1.0" encoding="utf-8"?>
<group>group1</group><List>
<cars_1>
<car>
<color_info>green</color_info>
<brand_name>nissan</brand_name>
</car>
<car>
<color_info>yellow</color_info>
<brand_name>renault</brand_name>
</car>
<car>
<color_info>black</color_info>
<brand_name>ford</brand_name>
</car>
</cars_1>
<cars_2>
<car>
<color_info>blu</color_info>
<brand_name>porsche</brand_name>
</car>
<car>
<color_info>white</color_info>
<brand_name>volvo</brand_name>
</car>
</cars_2>
</List><group>group1</group><List>
<cars_1>
<car>
<color_info>blu</color_info>
<brand_name>porsche</brand_name>
</car>
<car>
<color_info>yellow</color_info>
<brand_name>renault</brand_name>
</car>
<car>
<color_info>black</color_info>
<brand_name>ford</brand_name>
</car>
</cars_1>
<cars_2>
<car>
<color_info>blu</color_info>
<brand_name>porsche</brand_name>
</car>
<car>
<color_info>white</color_info>
<brand_name>volvo</brand_name>
</car>
</cars_2>
</List>
使用我的xsl文件获取:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:output method="xml" indent="yes" encoding="utf-8" />
<xsl:template match="/" >
<xsl:for-each select="group/List">
<group>group1</group>
<List>
<cars_1>
<car>
<color_info><xsl:value-of select="value/color"/>
</color_info>
<brand_name><xsl:value-of select="value/name"/></brand_name>
</car>
<car>
<color_info>yellow</color_info>
<brand_name>renault</brand_name>
</car>
<car>
<color_info>black</color_info>
<brand_name>ford</brand_name>
</car>
</cars_1>
<cars_2>
<car>
<color_info>blu</color_info>
<brand_name>porsche</brand_name>
</car>
<car>
<color_info>white</color_info>
<brand_name>volvo</brand_name>
</car>
</cars_2>
</List>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
提前致谢
答案 0 :(得分:0)
好吧,如果你这样做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/group" >
<group>
<xsl:for-each select="List">
<cars id="{id_num}">
<xsl:for-each select="value">
<car>
<color_info>
<xsl:value-of select="color"/>
</color_info>
<brand_name>
<xsl:value-of select="name"/>
</brand_name>
</car>
</xsl:for-each>
</cars>
</xsl:for-each>
</group>
</xsl:template>
</xsl:stylesheet>
你会得到:
<?xml version="1.0" encoding="UTF-8"?>
<group>
<cars id="1">
<car>
<color_info>green</color_info>
<brand_name>nissan</brand_name>
</car>
<car>
<color_info>yellow</color_info>
<brand_name>renault</brand_name>
</car>
<car>
<color_info>black</color_info>
<brand_name>ford</brand_name>
</car>
</cars>
<cars id="2">
<car>
<color_info>blu</color_info>
<brand_name>porsche</brand_name>
</car>
<car>
<color_info>white</color_info>
<brand_name>volvo</brand_name>
</car>
</cars>
</group>
我相信它与预期结果足够接近。