-1

I am facing a problem in transformation of XML. I need to transform an output in text file as with ',' separated. I need to do the total based on type Descriptor.

Output should look like:

Co code ID    type   amount
DEEP1   12345  phone  14
DEEP1   12345  food   8
DEEP1   12346  phone  16

XML:

<Root>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Phone"></Type>
<amount>8</amount>
</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>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>
</Root>

Please let me know if need more information.

Ram
  • 3,092
  • 10
  • 40
  • 56
  • This is a *grouping* problem. Do a search: it's one of the most often asked questions here. Note that solutions are different for XSLT 1.0 or 2.0 (you should not tag your question as both). – michael.hor257k Dec 07 '14 at 17:18
  • Thanks Michael, My system supports XSLT 2.0 . So edited the question tag.And also i have searched and based on that i was testing but i am not ablr to achieve it. – Deepak singh Dec 07 '14 at 17:52

1 Answers1

0

It is not quite clear whether an Employees element can contain several Employee elements. If that is not the case then you can group as follows:

<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>&#10;</xsl:text>
  <xsl:for-each-group select="Employees" group-adjacent="Type/@Descriptor">
    <xsl:if test="position() gt 1"><xsl:text>&#10;</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>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you so much. I have edited the xml file. XML that i have included Element structure is employee detaits8. I will try this solution and you know the result. – Deepak singh Dec 07 '14 at 18:07
  • Hi Martin, This solved my half issue. But the logic slightly got changed . I have posted a new question Liknk: http://stackoverflow.com/questions/27346820/grouping-based-on-conditions-and-putting-them-into-a-single-row-in-xslt-transfor. Could you please look once and Let me know your thoughts if that possible or not. If yes how i should achieve that. Thank you so much :) – Deepak singh Dec 07 '14 at 19:28