• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Eliminating duplication of node data in XSLT 1.0

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks in Advance & Sorry as it might take a while for you to understand my logic (i'm not a pro in using XSLT).

Could anyone help in eliminating duplication of tag "PaySlip_Val" and group all "ListOfPaySlipData" childs under one parent "ListOfPaySlipMonth".


I'm using XSLT in CRM system where we use base XML data & XSL code to get the desired result. I've madeup the XML to match my requirement. My req is "to group all the 'Details' under one parent 'ListOfPaySlipMonth' when the 'PaySlip_Val' is y or yes. But the result has data both y & yes in PaySlip_Val. Instead I want one tag PaySlip_Val and group all 'Details' grouped under it.

Please find the attached XML Data, XSLT Code , Actual result & Desired result.

XML Data

<?xml version="1.0" encoding="UTF-8"?>
<Data>
<P>
<FstName>F1</FstName>
<LstName>L1</LstName>
<Type>
<Payslip>y</Payslip>
<Details>
<Year>2016</Year>
<Month>Jan</Month>
<Amount>$$$</Amount>
</Details>
<Details>
<Year>2016</Year>
<Month>Feb</Month>
<Amount>$$$</Amount>
</Details>
</Type>
<Type>
<Payslip>yes</Payslip>
<Details>
<Year>2016</Year>
<Month>Mar</Month>
<Amount>$$$</Amount>
</Details>
<Details>
<Year>2016</Year>
<Month>Apr</Month>
<Amount>$$$</Amount>
</Details>
</Type>
<Type>
<Payslip>n</Payslip>
<Details>
<Year>2016</Year>
<Month>May</Month>
<Amount>$$$</Amount>
</Details>
</Type>
</P>
<P>
<FstName>F2</FstName>
<LstName>L2</LstName>
<Type>
<Payslip>n</Payslip>
<Details>
<Year>2016</Year>
<Month>Feb</Month>
<Leaves>4</Leaves>
</Details>
</Type>
</P>
</Data>

XSLT Code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:template match="/">
<xsl:call-template name="Pay_Slip" />
</xsl:template>
<xsl:template name="Pay_Slip">
<xsl:element name="ListOfData">
<xsl:for-each select="Data/P">
<xsl:element name="First_Name">
<xsl:value-of select="FstName" />
</xsl:element>
<xsl:element name="Last_Name">
<xsl:value-of select="LstName" />
</xsl:element>
<xsl:element name="PaySlip">
<xsl:for-each select="Type">
<xsl:variable name="Type" select="Payslip" />
<xsl:if test="$Type ='y' or $Type ='yes'">
<xsl:element name="PaySlip_Val">
<xsl:value-of select="$Type" />
</xsl:element>
<xsl:element name="ListOfPaySlipMonth">
<xsl:for-each select="Details">
<xsl:element name="ListOfPaySlipData">
<xsl:element name="Month">
<xsl:value-of select="Month" />
</xsl:element>
<xsl:element name="Salary">
<xsl:value-of select="Amount" />
</xsl:element>
<xsl:element name="Year">
<xsl:value-of select="Year" />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Actual result

<?xml version="1.0" encoding="UTF-8"?>
<ListOfData>
<First_Name>F1</First_Name>
<Last_Name>L1</Last_Name>
<PaySlip>
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<ListOfPaySlipData>
<Month>Jan</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Feb</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
</ListOfPaySlipMonth>
<PaySlip_Val>yes</PaySlip_Val>
<ListOfPaySlipMonth>
<ListOfPaySlipData>
<Month>Mar</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Apr</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
</ListOfPaySlipMonth>
</PaySlip>
<First_Name>F2</First_Name>
<Last_Name>L2</Last_Name>
<PaySlip/>
</ListOfData>

Expected Result:

<ListOfData>
<First_Name>F1</First_Name>
<Last_Name>L1</Last_Name>
<PaySlip>
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<ListOfPaySlipData>
<Month>Jan</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Feb</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Mar</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Apr</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
</ListOfPaySlipMonth>
</PaySlip>
<First_Name>F2</First_Name>
<Last_Name>L2</Last_Name>
<PaySlip/>
</ListOfData>

 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is generally under the category of grouping problem. On all generality and for bigger data set, in xslt 1.0, you can look into the use of xsl:key. But don't worry, it is doable without, without sacrificing efficiency for a smaller data set.

First observation is the construction of ListOfPaySlipData is kind of constant for type 'yes' or 'y', so you put the construction in a named template, say 'process' hereinbelow. Then second observation is that PaySlip_Val and ListOfPaySlipMonth are to appear only once. The ListOfPaySlipMonth structure is more involved that is why a call to a named template is necessary for an orderly template scripting. In any case, they appear at the first occurrence of Type 'yes' or 'y'. This is now determined by simply counting of preceding-sibling of the similar kind of Type.

This is how.
 
reply
    Bookmark Topic Watch Topic
  • New Topic