-1

I am facing problem to divide payment history string like

"Apr:2013,XXX/STD|Jan:2013,XXX/STD|Dec:2012,XXX/STD|Nov:2012,XXX/STD|"

to below xml element as below

<Periods>
<Period Year=2013>
     <Months month1="Apr" month1Value="XXX/STD" month2="Jan" month2Value="XXX/STD" 
</Period >
<Period Year=2012>
     <Months month1="Dec" month1Value="XXX/STD" month2="Nov" month2Value="XXX/STD" 
</Period>
</Periods>

From comments

lets assume my exact payment history as below "Apr:2013,XXX/STD|Mar:2013,XXX/STD|Feb:2013,XXX/STD|Jan:2013,XXX/STD|Dec:2012,XXX/STD|Nov:2012,XXX/STD|Oct:2012,XXX/STD|Sep:2012,XXX/STD|Aug:2012,XXX/STD|Jul:2012,XXX/STD|Jun:2012,XXX/STD|May:2012,XXX/STD|Apr:2012,XXX/STD|Mar:2012,XXX/STD|" but now its generating duplicates nodes. Please suggest

Alejandro
  • 1,882
  • 6
  • 13

2 Answers2

0

Assuming your string will contain payment history categorized by year.

What below code does:

1. It will split string by every second delimiter '|'

e.g. It will generate

<temp>Apr:2013,XXX/STD|Jan:2013,XXX/STD</temp>
<temp>Dec:2012,XXX/STD|Nov:2012,XXX/STD</temp>

2. Further, it will grab values from each <temp> iterating through the generated array stored in variable values.

Here you go:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
            xmlns:exsl="http://exslt.org/common" version="1.0"
            exclude-result-prefixes="exsl">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="payment_history">

        <xsl:variable name="values">
            <xsl:call-template name="split">
                <xsl:with-param name="pText" select="."/>
            </xsl:call-template>
        </xsl:variable>

        <Periods>
            <xsl:for-each select="exsl:node-set($values)/*">
                <Period Year="{substring-before(substring-after(normalize-space(.),':'),',')}">
                    <Months month1="{substring-before(.,':')}" 
                            month1Value="{substring-before(substring-after(normalize-space(.),','),'|')}"
                            month2="{substring-before(substring-after(normalize-space(.),'|'),':')}" 
                            month2Value="{substring-after(substring-after(normalize-space(.),'|'),',')}" />
                </Period>
            </xsl:for-each>
        </Periods>
</xsl:template>

<xsl:template match="text()" name="split">
    <xsl:param name="pText" select="."/>

    <xsl:if test="normalize-space($pText)">
        <xsl:variable name="vText" select="normalize-space($pText)"/>
        <temp> 
            <xsl:value-of select="substring-before($vText, '|')"/>
            <xsl:value-of select="concat('|', substring-before(substring-after($vText, '|'), '|'))"/>
        </temp>
        <xsl:call-template name="split">
            <xsl:with-param name="pText" select="substring-after(substring-after($vText, '|'), '|')"/>
        </xsl:call-template>
    </xsl:if>
  </xsl:template>

  </xsl:stylesheet>

http://xsltfiddle.liberty-development.net/bnnZW3

Vebbie
  • 1,669
  • 2
  • 12
  • 18
  • Hi Vebbie , lets assume my exact payment history as below "Apr:2013,XXX/STD|Mar:2013,XXX/STD|Feb:2013,XXX/STD|Jan:2013,XXX/STD|Dec:2012,XXX/STD|Nov:2012,XXX/STD|Oct:2012,XXX/STD|Sep:2012,XXX/STD|Aug:2012,XXX/STD|Jul:2012,XXX/STD|Jun:2012,XXX/STD|May:2012,XXX/STD|Apr:2012,XXX/STD|Mar:2012,XXX/STD|" but now its generating duplicates nodes. Please suggest – Nikhil Karande Mar 19 '19 at 11:31
  • I think it is not creating duplicates. But if your string is having more than 2 delimited string of same year than you have to show how your output should look like in above case. – Vebbie Mar 19 '19 at 11:44
  • @NikhilKarande : It's already populating what you want. Do you need to populate all `` tags in same `` for a same year value ? – Vebbie Mar 19 '19 at 12:21
  • Thanx for your suggestions – Nikhil Karande Mar 20 '19 at 10:37
0

Try it this way:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:param name="input"/>

<xsl:template match="/">
    <Periods>
        <xsl:call-template name="tokenize-to-pairs">
            <xsl:with-param name="text" select="$input"/>
        </xsl:call-template>
    </Periods>
</xsl:template>

<xsl:template name="tokenize-to-pairs">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'|'"/>
    <xsl:if test="contains($text, $delimiter) and contains(substring-after($text, $delimiter), $delimiter)">
        <xsl:call-template name="parse-period">
            <xsl:with-param name="month1" select="substring-before($text, $delimiter)"/>
            <xsl:with-param name="month2" select="substring-before(substring-after($text, $delimiter), $delimiter)"/>
        </xsl:call-template>
         <!-- recursive call -->
        <xsl:call-template name="tokenize-to-pairs">
            <xsl:with-param name="text" select="substring-after(substring-after($text, $delimiter), $delimiter)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

<xsl:template name="parse-period">
    <xsl:param name="month1"/>
    <xsl:param name="month2"/>
    <Period Year="{substring-before(substring-after($month1, ':'), ',')}">
        <Months 
            month1="{substring-before($month1, ':')}" 
            month1Value="{substring-after($month1, ',')}" 
            month2="{substring-before($month2, ':')}" 
            month2Value="{substring-after($month2, ',')}"/>
    </Period >
</xsl:template>

</xsl:stylesheet>

When this stylesheet is called with an input parameter of:

Apr:2013,XXX/STD|Mar:2013,XXX/STD|Feb:2013,XXX/STD|Jan:2013,XXX/STD|Dec:2012,XXX/STD|Nov:2012,XXX/STD|Oct:2012,XXX/STD|Sep:2012,XXX/STD|Aug:2012,XXX/STD|Jul:2012,XXX/STD|Jun:2012,XXX/STD|May:2012,XXX/STD|Apr:2012,XXX/STD|Mar:2012,XXX/STD|

the result will be:

<Periods>
  <Period Year="2013">
    <Months month1="Apr" month1Value="XXX/STD" month2="Mar" month2Value="XXX/STD"/>
  </Period>
  <Period Year="2013">
    <Months month1="Feb" month1Value="XXX/STD" month2="Jan" month2Value="XXX/STD"/>
  </Period>
  <Period Year="2012">
    <Months month1="Dec" month1Value="XXX/STD" month2="Nov" month2Value="XXX/STD"/>
  </Period>
  <Period Year="2012">
    <Months month1="Oct" month1Value="XXX/STD" month2="Sep" month2Value="XXX/STD"/>
  </Period>
  <Period Year="2012">
    <Months month1="Aug" month1Value="XXX/STD" month2="Jul" month2Value="XXX/STD"/>
  </Period>
  <Period Year="2012">
    <Months month1="Jun" month1Value="XXX/STD" month2="May" month2Value="XXX/STD"/>
  </Period>
  <Period Year="2012">
    <Months month1="Apr" month1Value="XXX/STD" month2="Mar" month2Value="XXX/STD"/>
  </Period>
</Periods>

Note that the year of the period is the year of the first month.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51