Question

I am trying to compound immediate following siblings with the same values together. But I am having trouble to select only the IMMEDIATE siblings.

Input:

<ROWS>
    <ROW>
        <MONTH>1</MONTH>
        <START_DATE>15/04/2013</START_DATE>
        <RATE_AMOUNT>10</RATE_AMOUNT>
        <DISCOUNT>-2</DISCOUNT>
    </ROW>
    <ROW>
        <MONTH>2</MONTH>
        <START_DATE>15/05/2013</START_DATE>
        <RATE_AMOUNT>10</RATE_AMOUNT>
        <DISCOUNT>-2</DISCOUNT>
    </ROW>
    <ROW>
        <MONTH>3</MONTH>
        <START_DATE>15/06/2013</START_DATE>
        <RATE_AMOUNT>10</RATE_AMOUNT>
        <DISCOUNT>-5</DISCOUNT>
    </ROW>
    <ROW>
        <MONTH>4</MONTH>
        <START_DATE>15/07/2013</START_DATE>
        <RATE_AMOUNT>10</RATE_AMOUNT>
        <DISCOUNT>-2</DISCOUNT>
    </ROW>
</ROWS>

Expected Output:

<RateList>
    <Rate>
        <NoOfMonths>2</NoOfMonths>
        <StartDate>15/04/2013</StartDate>
        <RateAmount>10</RateAmount>
        <Discount>-2</Discount>
    </Rate>
    <Rate>
        <NoOfMonths>1</NoOfMonths>
        <StartDate>15/06/2013</StartDate>
        <RateAmount>10</RateAmount>
        <Discount>-5</Discount>
    </Rate>
    <Rate>
        <NoOfMonths>1</NoOfMonths>
        <StartDate>15/07/2013</StartDate>
        <RateAmount>10</RateAmount>
        <Discount>-2</Discount>
    </Rate>
</RateList>

And this is my XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <RateList>
            <xsl:apply-templates/>
        </RateList>
    </xsl:template>
    <xsl:template match="ROW">
        <xsl:variable name="noOfMonths" select=".|following-sibling::*[RATE_AMOUNT=current()/RATE_AMOUNT][DISCOUNT=current()/DISCOUNT]"/>
        <xsl:if test="not(preceding-sibling::*[RATE_AMOUNT=current()/RATE_AMOUNT][DISCOUNT=current()/DISCOUNT])"> 
            <Rate>
                <NoOfMonths>
                    <xsl:value-of select="count($noOfMonths)"/>
                </NoOfMonths>       
                <StartDate>
                    <xsl:value-of select="START_DATE"/>
                </StartDate>
                <RateAmount>
                    <xsl:value-of select="RATE_AMOUNT"/>
                </RateAmount>
                <Discount>
                    <xsl:value-of select="DISCOUNT"/>
                </Discount>
            </Rate>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

And this is the output that I am getting:

<RateList>
    <Rate>
        <NoOfMonths>3</NoOfMonths>
        <StartDate>15/04/2013</StartDate>
        <RateAmount>10</RateAmount>
        <Discount>-2</Discount>
    </Rate>
    <Rate>
        <NoOfMonths>1</NoOfMonths>
        <StartDate>15/06/2013</StartDate>
        <RateAmount>10</RateAmount>
        <Discount>-5</Discount>
    </Rate>
</RateList>

Can someone help, please? How do I select/count only the immediate siblings?

Thank you!

Was it helpful?

Solution

Try this (Some explanation as comment in xslt):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <RateList>
            <xsl:apply-templates select="*/ROW"/>
        </RateList>
    </xsl:template>
    <xsl:template match="ROW">

        <!--Look for preceding row which has not the same data (RATE_AMOUNT and DISCOUNT ) as the current. 
        And generate a id, here month concatenated with an '#'.-->
        <xsl:variable name="notSameData"
                  select="concat(preceding-sibling::ROW
                      [not(RATE_AMOUNT=current()/RATE_AMOUNT 
                      and DISCOUNT=current()/DISCOUNT )][1]/MONTH,'#')"/>

        <!--Count following month which has same data as current
        and also the same preceding month with not the same data as the current-->
        <xsl:variable name="noOfMonths"
                      select="count(following-sibling::*
                            [  RATE_AMOUNT=preceding-sibling::*[1]/RATE_AMOUNT and
                              DISCOUNT = preceding-sibling::*[1]/DISCOUNT]
                            [
                                  concat(preceding-sibling::ROW
                                  [not(RATE_AMOUNT=current()/RATE_AMOUNT 
                                  and DISCOUNT=current()/DISCOUNT )][1]/MONTH,'#') = $notSameData
                             ]) +1 "/>

        <!--Output only for rows which don not have a not a direct (first) preceding  one with same data.-->
        <xsl:if test="not(preceding-sibling::ROW[1][RATE_AMOUNT=current()/RATE_AMOUNT][DISCOUNT=current()/DISCOUNT])">
                <Rate>
                <NoOfMonths>
                    <xsl:value-of select="$noOfMonths"/>
                </NoOfMonths>
                <StartDate>
                    <xsl:value-of select="START_DATE"/>
                </StartDate>
                <RateAmount>
                    <xsl:value-of select="RATE_AMOUNT"/>
                </RateAmount>
                <Discount>
                    <xsl:value-of select="DISCOUNT"/>
                </Discount>
            </Rate>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Which will generate the following output:

<?xml version="1.0"?>
<RateList>
  <Rate>
    <NoOfMonths>2</NoOfMonths>
    <StartDate>15/04/2013</StartDate>
    <RateAmount>10</RateAmount>
    <Discount>-2</Discount>
  </Rate>
  <Rate>
    <NoOfMonths>1</NoOfMonths>
    <StartDate>15/06/2013</StartDate>
    <RateAmount>10</RateAmount>
    <Discount>-5</Discount>
  </Rate>
  <Rate>
    <NoOfMonths>1</NoOfMonths>
    <StartDate>15/07/2013</StartDate>
    <RateAmount>10</RateAmount>
    <Discount>-2</Discount>
  </Rate>
</RateList>

Comment: I did not use xlt:key because my favorite xlst processor xsltproc does not support current() in xls:key statements.

OTHER TIPS

By immediate sibling I understand the siblings just above and below . In this case , you could use the position() function to restrict . I have just started learning this myself , but it could be something like

following-sibling::*[1] 

and

preceding-sibling::*[1]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top