Reputation: 15
I am facing a problem in transformation of XML. I need to transform an output in csv file as with ',' seperated.
XML:
<Root>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Phone"></Type>
<amount1>8</amount1>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Phone"></Type>
<amount>6</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Food"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Travel"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Other"></Type>
<amount>800</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12346</ID>
</Employee>
<Type Descriptor="Phone"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12346</ID>
</Employee>
<Type Descriptor="Phone"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12346</ID>
</Employee>
<Type Descriptor="Other"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12346</ID>
</Employee>
<Type Descriptor="Food"></Type>
<amount>8</amount>
</Employees>
</Root>
Currently using XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:param name="sep" select="', '"/>
<xsl:output method="text"/>
<xsl:template match="Root">
<xsl:value-of select="'Co Code', 'ID', 'type', 'amount'" separator="{$sep}"/>
<xsl:text> </xsl:text>
<xsl:for-each-group select="Employees" group-adjacent="Type/@Descriptor">
<xsl:if test="position() gt 1"><xsl:text> </xsl:text></xsl:if>
<xsl:value-of select="Employee/Co_Code, Employee/ID, current-grouping-key(), sum(current-group()/amount)"
separator="{$sep}"/>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Currently generating the Output in below format:
Co Code, ID, type, amount
DEEP1, 12345, Phone, 6
DEEP1, 12345, Food, 8
DEEP1, 12345, Travel, 8
DEEP1, 12345, Other, 800
DEEP1, 12346, Phone, 16
DEEP1, 12346, Other, 8
DEEP1, 12346, Food, 8
But i need to arrange the few row into column . I am trying to get the output in below format. Employee Sum of Phone and Food should be written in one seperate column and For other type it should create a seperate line with Phone and Food column as blank.
Co Code, ID, Phone , Food, type, amount
DEEP1, 12345, 6 , 8 , ,
DEEP1, 12345, , , Travel, 8
DEEP1, 12345, Other, 800
DEEP1, 12346, 16 , 8 , ,
DEEP1, 12346, , , Other, 8
Please give me some idea to achieve this.
Thanks to Martin Honnen for helping me.
Upvotes: 0
Views: 619
Reputation: 70648
You probably want to start off by grouping by the Employee's ID
<xsl:for-each-group select="Employees"
group-by="Employee/ID">
You then have a specific line for outputting the "Food" and "Phone" values
<xsl:value-of select="Employee/Co_Code,
current-grouping-key(),
sum(current-group()[Type/@Descriptor = 'Phone']/amount),
sum(current-group()[Type/@Descriptor = 'Food']/amount),
'',
''" separator="{$sep}"/>
And within the current group, you can then group the non "Food" and "Phone" entries by their descriptor (This would only be necessary if the descriptors can be repeated)
<xsl:for-each-group select="current-group()[not(Type/@Descriptor = ('Phone', 'Food'))]"
group-by="Type/@Descriptor">
Then it is a straight-forward case of outputting a line for this too.
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:param name="sep" select="', '"/>
<xsl:output method="text"/>
<xsl:template match="Root">
<xsl:value-of select="'Co Code', 'ID', 'type', 'amount'" separator="{$sep}"/>
<xsl:text> </xsl:text>
<xsl:for-each-group select="Employees" group-by="Employee/ID">
<xsl:value-of select="Employee/Co_Code, current-grouping-key(), sum(current-group()[Type/@Descriptor = 'Phone']/amount), sum(current-group()[Type/@Descriptor = 'Food']/amount), '', ''" separator="{$sep}"/>
<xsl:text> </xsl:text>
<xsl:for-each-group select="current-group()[not(Type/@Descriptor = ('Phone', 'Food'))]" group-by="Type/@Descriptor">
<xsl:value-of select="Employee/Co_Code, '', '', sum(current-group()/amount), current-group()/Type/@Descriptor" separator="{$sep}"/>
<xsl:text> </xsl:text>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1