xslt - Rows coming extra in output xml -
i have input xml -
<sample> <sample_id>111</sample_id> <rows> <row row_type="specification" row_id="1"> <price_per_unit> <amount sign="+" vat="included">1025.00</amount> <amount sign="+" vat="excluded">949.07</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">1025.00</amount> <amount sign="+" vat="excluded">949.07</amount> </row_total> </row> <row row_type="specification" row_id="1"> <price_per_unit> <amount sign="+" vat="included">67.00</amount> <amount sign="+" vat="excluded">53.60</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">67.00</amount> <amount sign="+" vat="excluded">53.60</amount> </row_total> </row> <row row_type="specification" row_id="1"> <price_per_unit> <amount sign="+" vat="included">3.00</amount> <amount sign="+" vat="excluded">2.40</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">3.00</amount> <amount sign="+" vat="excluded">2.40</amount> </row_total> </row> <row row_type="subtotal" row_id="1"> <price_per_unit> <amount sign="+" vat="included">1095.00</amount> <amount sign="+" vat="excluded">0.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">1095.00</amount> <amount sign="+" vat="excluded">0.00</amount> </row_total> </row> <row row_type="specification" row_id="2"> <price_per_unit> <amount sign="+" vat="included">315.00</amount> <amount sign="+" vat="excluded">291.67</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">630.00</amount> <amount sign="+" vat="excluded">583.33</amount> </row_total> </row> <row row_type="specification" row_id="2"> <price_per_unit> <amount sign="+" vat="included">30.00</amount> <amount sign="+" vat="excluded">24.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">60.00</amount> <amount sign="+" vat="excluded">48.00</amount> </row_total> </row> <row row_type="specification" row_id="2"> <price_per_unit> <amount sign="+" vat="included">10.00</amount> <amount sign="+" vat="excluded">8.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">20.00</amount> <amount sign="+" vat="excluded">16.00</amount> </row_total> </row> <row row_type="specification" row_id="2"> <price_per_unit> <amount sign="+" vat="included">175.00</amount> <amount sign="+" vat="excluded">140.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">350.00</amount> <amount sign="+" vat="excluded">280.00</amount> </row_total> </row> <row row_type="subtotal" row_id="2"> <price_per_unit> <amount sign="+" vat="included">530.00</amount> <amount sign="+" vat="excluded">0.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">1060.00</amount> <amount sign="+" vat="excluded">0.00</amount> </row_total> </row> </rows>
my conversion looks -
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="krowbyid" match="row[not(@row_type='subtotal' or @row_type='main' or @row_type='comment')]" use="@row_id"/> <xsl:template match="row[not(@row_type='specification')]"> <xsl:copy-of select="."/> <xsl:copy-of select="key('krowbyid', @row_id)"/> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>
my output structure coming correct issue is, logic adding repeating rows in output xml. not required. suggestions on this.
expected output -
<sample> <row row_type="subtotal" row_id="1"> <price_per_unit> <amount sign="+" vat="included">1095.00</amount> <amount sign="+" vat="excluded">0.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">1095.00</amount> <amount sign="+" vat="excluded">0.00</amount> </row_total> </row> <row row_type="specification" row_id="1"> <price_per_unit> <amount sign="+" vat="included">1025.00</amount> <amount sign="+" vat="excluded">949.07</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">1025.00</amount> <amount sign="+" vat="excluded">949.07</amount> </row_total> </row> <row row_type="specification" row_id="1"> <price_per_unit> <amount sign="+" vat="included">67.00</amount> <amount sign="+" vat="excluded">53.60</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">67.00</amount> <amount sign="+" vat="excluded">53.60</amount> </row_total> </row> <row row_type="specification" row_id="1"> <price_per_unit> <amount sign="+" vat="included">3.00</amount> <amount sign="+" vat="excluded">2.40</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">3.00</amount> <amount sign="+" vat="excluded">2.40</amount> </row_total> </row> <row row_type="subtotal" row_id="2"> <price_per_unit> <amount sign="+" vat="included">530.00</amount> <amount sign="+" vat="excluded">0.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">1060.00</amount> <amount sign="+" vat="excluded">0.00</amount> </row_total> </row> <row row_type="specification" row_id="2"> <price_per_unit> <amount sign="+" vat="included">315.00</amount> <amount sign="+" vat="excluded">291.67</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">630.00</amount> <amount sign="+" vat="excluded">583.33</amount> </row_total> </row> <row row_type="specification" row_id="2"> <price_per_unit> <amount sign="+" vat="included">30.00</amount> <amount sign="+" vat="excluded">24.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">60.00</amount> <amount sign="+" vat="excluded">48.00</amount> </row_total> </row> <row row_type="specification" row_id="2"> <price_per_unit> <amount sign="+" vat="included">10.00</amount> <amount sign="+" vat="excluded">8.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">20.00</amount> <amount sign="+" vat="excluded">16.00</amount> </row_total> </row> <row row_type="specification" row_id="2"> <price_per_unit> <amount sign="+" vat="included">175.00</amount> <amount sign="+" vat="excluded">140.00</amount> </price_per_unit> <row_total> <amount sign="+" vat="included">350.00</amount> <amount sign="+" vat="excluded">280.00</amount> </row_total> </row> </sample>
because question has 1 "sample" element, not entirely clear how want multiple "sample" elements, thinking need extend xsl:key include sample_id element.
<xsl:key name="krowbyid" match="row[not(@row_type='subtotal' or @row_type='main' or @row_type='comment')]" use="concat(@row_id, '|', ../../sample_id)"/>
so, key uses both row_id , sample_id. note use of |
character delimiter; can character here, long not appear in row_id or sample_id.
then, use same key in existing xsl:copy when getting row elements sample.
<xsl:copy-of select="key('krowbyid', concat(@row_id, '|', ../../sample_id))"/>
try xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="krowbyid" match="row[not(@row_type='subtotal' or @row_type='main' or @row_type='comment')]" use="concat(@row_id, '|', ../../sample_id)"/> <xsl:template match="row[not(@row_type='specification')]"> <xsl:copy-of select="."/> <xsl:copy-of select="key('krowbyid', concat(@row_id, '|', ../../sample_id))"/> </xsl:template> <xsl:template match="text()"/> <xsl:template match="sample"> <xsl:copy> <xsl:apply-templates /> </xsl:copy> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment