I have tried transforming an xml file using XSLT, however i am not getting the desired output that i need. Could someone help changing or providing a new xslt? Thanks for your help, any version of xslt will do.
This is my original XML file :
col1 is the ID
col2 is the User #
col3 is the date
col4 is the amount
col5 is the currency
<root>
<row>
<col1>JBC</col1>
<col2>00001</col2>
<col3>2024-06-01</col3>
<col4>200</col4>
<col5>EUR</col5>
</row>
<row>
<col1>JBC</col1>
<col2>00002</col2>
<col3>2024-06-01</col3>
<col4>300</col4>
<col5>EUR</col5>
</row>
<row>
<col1>JBC</col1>
<col2>00003</col2>
<col3>2024-06-05</col3>
<col4>400</col4>
<col5>EUR</col5>
</row>
<row>
<col1>JBC</col1>
<col2>00002</col2>
<col3>2024-06-05</col3>
<col4>350</col4>
<col5>EUR</col5>
</row>
<row>
<col1>XYZ</col1>
<col2>00004</col2>
<col3>2024-06-08</col3>
<col4>600</col4>
<col5>USD</col5>
</row>
</root>
Desired Output
<root>
<row> <!-- this row will be copied as is -->
<col1>JBC</col1>
<col2>00001</col2>
<col3>2024-06-01</col3>
<col4>200</col4>
<col5>EUR</col5>
</row>
<row> <!-- this row will get the value of col4 from the next sibling that have the equal col2 -->
<col1>JBC</col1>
<col2>00002</col2>
<col3>2024-06-01</col3>
<col4>350</col4>
<col5>EUR</col5>
</row>
<row> <!-- this row will be copied as is -->
<col1>JBC</col1>
<col2>00003</col3>
<col3>2024-06-05</col3>
<col4>400</col4>
<col5>EUR</col5>
</row>
<row> <!-- this row will be copied but with newer date -->
<col1>JBC</col1>
<col2>00002</col2>
<col3>2024-06-05</col3>
<col4>350</col4>
<col5>EUR</col5>
</row>
<row> <!-- this row will be copied but with newer date -->
<col1>JBC</col1>
<col2>00001</col2>
<col3>2024-06-05</col3>
<col4>200</col4>
<col5>EUR</col5>
</row>
<row> <!-- this row will be copied as is -->
<col1>XYZ</col1>
<col2>00004</col2>
<col3>2024-06-08</col3>
<col4>600</col4>
<col5>USD</col5>
</row>
</root>
So far this is the xslt that i am using
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- Key to group rows by date and col1 -->
<xsl:key name="rows-by-date" match="row" use="concat(col1, '|', col3)"/>
<xsl:template match="/root">
<root>
<!-- Process each unique (col1, date) pair -->
<xsl:for-each select="row[not(concat(col1, '|', col3) = preceding-sibling::row/concat(col1, '|', col3))]">
<xsl:sort select="col3" data-type="text"/>
<!-- Copy the rows for the current (col1, date) pair -->
<xsl:for-each select="key('rows-by-date', concat(col1, '|', col3))">
<xsl:copy-of select="."/>
</xsl:for-each>
<!-- Copy the rows for previous dates with the current date and col1 -->
<xsl:variable name="current-date" select="col3"/>
<xsl:variable name="current-col1" select="col1"/>
<xsl:for-each select="preceding-sibling::row[concat(col1, '|', col3) != concat($current-col1, '|', $current-date) and col1 = $current-col1]">
<row>
<col1><xsl:value-of select="col1"/></col1>
<col2><xsl:value-of select="col2"/></col2>
<col3><xsl:value-of select="$current-date"/></col3>
<col4><xsl:value-of select="col4"/></col4>
<col5><xsl:value-of select="col5"/></col5>
</row>
</xsl:for-each>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
New contributor
Jake Jang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.