I have a xml and I want to transform it into another xml. Looking for 2 days and have not found any good example for my case; 2 nodes cars and distances .
For each id_car, I have to group the distances (see below the output xml).
Source :
<?xml version="1.0" encoding="ISO-8859-1"?>
<output>
<cars>
<car>
<id>1</id>
<brand>Audi</brand>
<type>A4_Quattro</type>
<license>TEST</license>
</car>
<car>
<id>2</id>
<brand>FORD</brand>
<type>XLT_Ranger</type>
<license>PROTOTYPE</license>
</car>
</cars>
<distances>
<distance>
<id_car>1</id_car>
<date>20110901</date>
<distance>123</distance>
</distance>
<distance>
<id_car>1</id_car>
<date>20110902</date>
<distance>194</distance>
</distance>
<distance>
<id_car>2</id_car>
<date>20110907</date>
<distance>24</distance>
</distance>
<distance>
<id_car>2</id_car>
<date>20110915</date>
<distance>105</distance>
</distance>
</distances>
</output>
output xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<output>
<cars>
<car>
<id>1</id>
<brand>Audi</brand>
<type>A4_Quattro</type>
<distances>
<distance day="20110901">123</distance>
<distance day="20110902">194</distance>
</distances>
</car>
<car>
<id>2</id>
<brand>FORD</brand>
<type>XLT_Ranger</type>
<license>PROTOTYPE</license>
<distances>
<distance day="20110907">24</distance>
<distance day="20110915">105</distance>
</distances>
</car>
</cars>
<output>
This is the result of dozens of attempts:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="Dist_car" match="distances/distance" use="id_car" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<distances>
<xsl:apply-templates select="key('Dist_car', id)"/>
</distances>
</xsl:copy>
</xsl:template>
<xsl:template match="distance">
<distance day="{date}"><xsl:value-of select="distance"/></distance>
</xsl:template>
</xsl:stylesheet>
If anyone has any idea is welcome ...thanks a lot!
PS : i test the xsl with this one http://xslttest.appspot.com/ .
Your template matches too broadly, so it generates too many <distances>
elements and it also copies the original distances.
Try to narrow down the match rules. You can also copy sections of the source document using xsl:copy-of
.
I'm getting your desired output if I change your stylesheet a bit to:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="Dist_car" match="distances/distance" use="id_car" />
<xsl:template match="/">
<output>
<cars>
<xsl:apply-templates select="output/cars" />
</cars>
</output>
</xsl:template>
<xsl:template match="car">
<xsl:copy>
<xsl:copy-of select="@*|node()" />
<distances>
<xsl:apply-templates select="key('Dist_car', id)" />
</distances>
</xsl:copy>
</xsl:template>
<xsl:template match="distance">
<distance day="{date}">
<xsl:value-of select="distance" />
</distance>
</xsl:template>
</xsl:stylesheet>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.