1

I need to take an XML file and create a new output file. During creation of the output file, I need to total the quantities by material

This is my input file: -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
        <MATERIAL>Material1</MATERIAL>
        <SALES_DIST>FS</SALES_DIST>
        <REGION>North</REGION>
        <FIELDNM001>EA</FIELDNM001>
        <MONTH>2020-01-01</MONTH>
        <FIELDNM002>1</FIELDNM002>
    </record>
    <record>
        <MATERIAL>Material1</MATERIAL>
        <SALES_DIST>FS</SALES_DIST>
        <REGION>North</REGION>
        <FIELDNM001>EA</FIELDNM001>
        <MONTH>2020-01-01</MONTH>
        <FIELDNM002>1</FIELDNM002>
    </record>
</data-set>

What I need to end up with is a file like this: -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
        <MATERIAL>Material1</MATERIAL>
        <SALES_DIST>FS</SALES_DIST>
        <REGION>North</REGION>
        <FIELDNM001>EA</FIELDNM001>
        <MONTH>2020-01-01</MONTH>
        <FIELDNM002>2</FIELDNM002>
    </record>
</data-set>

Any ideas on what the xslt document should look like to achieve this?

Many thanks in advance.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
        <MATERIAL>Material1</MATERIAL>
        <SALES_DIST>FS</SALES_DIST>
        <REGION>North</REGION>
        <FIELDNM001>EA</FIELDNM001>
        <MONTH>2020-01-01</MONTH>
        <FIELDNM002>2</FIELDNM002>
    </record>
</data-set>
Neil
  • 13
  • 2
  • 1
    This is a *grouping* question. Do a search - it's probably the most often asked XSLT question here. Note that answers are different for XSLT 1.0 or 2.0. – michael.hor257k Jan 24 '19 at 07:16

1 Answers1

-1

Starting from this reply, the following works for me using xsltproc on Linux:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:gt="http://www.gtech.com/lsp/2009-09-23"
                exclude-result-prefixes="gt">

    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <record>
                <MATERIAL><xsl:value-of select="//MATERIAL" /></MATERIAL>
                <SALES_DIST><xsl:value-of select="//SALES_DIST" /></SALES_DIST>
                <REGION><xsl:value-of select="//REGION" /></REGION>
                <FIELDNM001><xsl:value-of select="//FIELDNM001" /></FIELDNM001>
                <MONTH><xsl:value-of select="//MONTH" /></MONTH>
                <FIELDNM002><xsl:value-of select="sum(//FIELDNM002[.])"/></FIELDNM002>
            </record>
        </data-set>
    </xsl:template>
</xsl:stylesheet>

If other fields of the records in xml are not equals, it takes only the first records' values

Manticore
  • 309
  • 2
  • 13
  • Thanks Manticore. That works a treat. However, as is often the case in the world of development, the goalposts have moved on me. I now need to total the quantity (FIELDNM002) when the tag AND the tag are the same. There could potentially be the same material occurring multiple times where the month values are different. So I need to group by Material and Month. I tried playing around with what you provided but can't seem to get it to work :( – Neil Jan 24 '19 at 22:49
  • your question was not complete then. Did you downvote ? – Manticore Jan 28 '19 at 17:20
  • No, I didn't down vote. Still not sure how all this works to be honest. Thy moved the goalposts on me though so I now need to group by 2 fields rather than one. Any idea how I can do that? – Neil Jan 29 '19 at 23:41
  • Although this is already 8 months ago, did you read https://stackoverflow.com/questions/42250394/xslt-2-0-grouping-summing-counting-and-conditions? But I guess you already solved the problem. – Stephan Sep 28 '19 at 06:38