0

I'm transforming the below XML to produce an text output. I Can able to successfully transform the XML. However, there is an additional new line getting printed which is not required in my case. I tried different way to avoid printing the unnecessary new line. However, i couldn't able to succeed. Basically i'm printing the data based on the condition in XSLT and if the last record from XSLT fails the condition then the new line remains which is created in the previous loop.

Below is my XML :

<?xml version='1.0' encoding='UTF-8'?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
        <wd:Get_EFW2_Year_End_Worker_Filing_Data_Response
            xmlns:wd="urn:com.workday/bsvc"
            wd:version="v28.0">
            <wd:Request_Criteria>
           <wd:Response_Data>
                <wd:EFW2_Year_End_Worker_Filing>
                    <wd:W-2_Instance_ID>3707$113701</wd:W-2_Instance_ID>
                    <wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
                    <wd:EFW2_Year_End_Worker_Filing_Data>
                        <wd:Employee_First_Name>Rowyna</wd:Employee_First_Name>
                        <wd:Employee_Middle_Initial>A</wd:Employee_Middle_Initial>
                        <wd:Employee_Last_Name>Brewer</wd:Employee_Last_Name>
                        <wd:EFW2_Additional_Data>
                            <wd:Statutory_Employee>0</wd:Statutory_Employee>
                            <wd:Retirement_Plan>0</wd:Retirement_Plan>
                            <wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
                        </wd:EFW2_Additional_Data>
                    </wd:EFW2_Year_End_Worker_Filing_Data>
                </wd:EFW2_Year_End_Worker_Filing>
                <wd:EFW2_Year_End_Worker_Filing>
                    <wd:W-2_Instance_ID>3707$113702</wd:W-2_Instance_ID>
                    <wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
                    <wd:EFW2_Year_End_Worker_Filing_Data>
                        <wd:Employee_First_Name>George</wd:Employee_First_Name>
                        <wd:Employee_Middle_Initial>E</wd:Employee_Middle_Initial>
                        <wd:Employee_Last_Name>Denbow</wd:Employee_Last_Name>
                        <wd:EFW2_Additional_Data>
                            <wd:Statutory_Employee>0</wd:Statutory_Employee>
                            <wd:Retirement_Plan>0</wd:Retirement_Plan>
                            <wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
                        </wd:EFW2_Additional_Data>
                        <wd:EFW2_Year_End_Worker_State_Data>
                            <wd:State_EIN>74-6000203-000</wd:State_EIN>
                            <wd:State>IL</wd:State>
                            <wd:State_FIPS_Code>17</wd:State_FIPS_Code>
                            <wd:State_Taxable_Wages>100.11</wd:State_Taxable_Wages>
                            <wd:State_Tax_Withheld>2004.34</wd:State_Tax_Withheld>
                        </wd:EFW2_Year_End_Worker_State_Data>
                    </wd:EFW2_Year_End_Worker_Filing_Data>
                </wd:EFW2_Year_End_Worker_Filing>
                <wd:EFW2_Year_End_Worker_Filing>
                    <wd:W-2_Instance_ID>3707$113703</wd:W-2_Instance_ID>
                    <wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
                    <wd:EFW2_Year_End_Worker_Filing_Data>
                        <wd:Employee_First_Name>Thomas</wd:Employee_First_Name>
                        <wd:Employee_Middle_Initial>B</wd:Employee_Middle_Initial>
                        <wd:Employee_Last_Name>Morris</wd:Employee_Last_Name>
                        <wd:EFW2_Additional_Data>
                            <wd:Statutory_Employee>0</wd:Statutory_Employee>
                            <wd:Retirement_Plan>0</wd:Retirement_Plan>
                            <wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
                        </wd:EFW2_Additional_Data>
                    </wd:EFW2_Year_End_Worker_Filing_Data>
                </wd:EFW2_Year_End_Worker_Filing>
                <wd:EFW2_Year_End_Worker_Filing>
                    <wd:W-2_Instance_ID>3707$113705</wd:W-2_Instance_ID>
                    <wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
                    <wd:EFW2_Year_End_Worker_Filing_Data>
                        <wd:Employee_First_Name>Gertrude</wd:Employee_First_Name>
                        <wd:Employee_Last_Name>Patton</wd:Employee_Last_Name>
                        <wd:EFW2_Additional_Data>
                            <wd:Statutory_Employee>0</wd:Statutory_Employee>
                            <wd:Retirement_Plan>0</wd:Retirement_Plan>
                            <wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
                        </wd:EFW2_Additional_Data>
                        <wd:EFW2_Year_End_Worker_State_Data>
                            <wd:State_EIN>74-6000203-000</wd:State_EIN>
                            <wd:State>IL</wd:State>
                            <wd:State_FIPS_Code>17</wd:State_FIPS_Code>
                            <wd:State_Taxable_Wages>100.11</wd:State_Taxable_Wages>
                            <wd:State_Tax_Withheld>2004.34</wd:State_Tax_Withheld>
                        </wd:EFW2_Year_End_Worker_State_Data>
                    </wd:EFW2_Year_End_Worker_Filing_Data>
                </wd:EFW2_Year_End_Worker_Filing>
                <wd:EFW2_Year_End_Worker_Filing>
                    <wd:W-2_Instance_ID>3707$113704</wd:W-2_Instance_ID>
                    <wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
                    <wd:EFW2_Year_End_Worker_Filing_Data>
                        <wd:Employee_First_Name>Roy</wd:Employee_First_Name>
                        <wd:Employee_Middle_Initial>L</wd:Employee_Middle_Initial>
                        <wd:Employee_Last_Name>Briseno</wd:Employee_Last_Name>
                        <wd:EFW2_Additional_Data>
                            <wd:Statutory_Employee>0</wd:Statutory_Employee>
                            <wd:Retirement_Plan>0</wd:Retirement_Plan>
                            <wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
                        </wd:EFW2_Additional_Data>
                    </wd:EFW2_Year_End_Worker_Filing_Data>
                </wd:EFW2_Year_End_Worker_Filing>
            </wd:Response_Data>
        </wd:Get_EFW2_Year_End_Worker_Filing_Data_Response>
    </env:Body>
</env:Envelope>

AND MY XSLT :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wd="urn:com.workday/bsvc"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
    xmlns:xtt="urn:com.workday/xtt" xmlns:etv="urn.com.workday/etv" version="2.0"
    xmlns:this="urn:BuildWorkerWageRecord_RW.xsl">

    <xsl:strip-space elements="*"/>

    <xsl:output method="text" indent="yes"/>
    <xsl:variable name="NEWLINE" select="'&#xD;&#xA;'"/>
    <xsl:variable name="delimeter">|</xsl:variable>
    <xsl:param name="prop.No.of.RS.Records"/>

    <xsl:variable name="Direction_Left">
        <xsl:text>LEFT</xsl:text>
    </xsl:variable>
    <xsl:variable name="Direction_Right">
        <xsl:text>RIGHT</xsl:text>
    </xsl:variable>
    <xsl:variable name="Record_Identifier">
        <xsl:text>RW</xsl:text>
    </xsl:variable>


    <xsl:template match="env:Envelope/env:Body/wd:Get_EFW2_Year_End_Worker_Filing_Data_Response">

        <xsl:for-each select="wd:Response_Data/wd:EFW2_Year_End_Worker_Filing">
        
           <!-- Get the state name to filter out the states other than Illinois -->
           <xsl:variable name="State" select="wd:EFW2_Year_End_Worker_Filing_Data/wd:EFW2_Year_End_Worker_State_Data[wd:State = 'IL']/wd:State"/>
           <xsl:if test="$State = 'IL'">            

            <!-- Record Identifier(RW)  -->
            <xsl:value-of select="$Record_Identifier"/>
            <xsl:value-of select="$delimeter"/>

            <!-- Social Security Number (SSN)  -->

            <xsl:variable name="SSN"
                select="translate(wd:EFW2_Year_End_Worker_Filing_Data/wd:SSN, '-', '')"/>

            <xsl:choose>
                <xsl:when test="$SSN = ''">
                    <xsl:value-of select="'000000000'"/>
                </xsl:when>
                <xsl:when test="substring($SSN, 1, 3) = '666'">
                    <xsl:value-of select="'000000000'"/>
                </xsl:when>
                <xsl:when test="substring($SSN, 1, 1) = '9'">
                    <xsl:value-of select="'000000000'"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$SSN"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:value-of select="$delimeter"/>

            <!-- Employee First Name -->
            <xsl:variable name="FirstName"
                select="wd:EFW2_Year_End_Worker_Filing_Data/wd:Employee_First_Name"/>
            <xsl:value-of select="this:pad($Direction_Right, 15, ' ', $FirstName)"/>
            <xsl:value-of select="$delimeter"/>

            <!-- Employee Middle Name or Initial -->
            <xsl:variable name="MiddleName"
                select="wd:EFW2_Year_End_Worker_Filing_Data/wd:Employee_Middle_Initial"/>
            <xsl:value-of select="this:pad($Direction_Right, 15, ' ', $MiddleName)"/>
            <xsl:value-of select="$delimeter"/>

            <!-- Employee Last Name -->
            <xsl:variable name="LastName"
                select="wd:EFW2_Year_End_Worker_Filing_Data/wd:Employee_Last_Name"/>
            <xsl:value-of select="this:pad($Direction_Right, 20, ' ', $LastName)"/>
            <xsl:value-of select="$delimeter"/>

            <!-- Wages, Tips and Other Compensation - BOX-1  -->
            <xsl:variable name="Box-1"
                select="wd:EFW2_Year_End_Worker_Filing_Data/wd:EFW2_Year_End_Worker_Box_Data[wd:Box_Description = 'Wages, tips, and other compensation']/wd:Amount"/>
            <xsl:choose>
                <xsl:when test="$Box-1 != ''">
                    <xsl:value-of
                        select="this:pad($Direction_Left, 11, '0', this:impliedDecimal(xs:string(format-number($Box-1, '0.00'))))"
                    />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'00000000000'"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:value-of select="$delimeter"/>

            <xsl:if test="position() != last()">
                <xsl:value-of select="$NEWLINE"/>
            </xsl:if>
          </xsl:if>
        </xsl:for-each>

    </xsl:template>

    <xsl:function name="this:pad" as="xs:string">
        <xsl:param name="direction" as="xs:string"/>
        <xsl:param name="length" as="xs:integer"/>
        <xsl:param name="padChar" as="xs:string"/>
        <xsl:param name="value"/>
        <xsl:variable name="padding_unreplaced">
            <xsl:text>                                                                                                                    </xsl:text>
        </xsl:variable>
        <xsl:variable name="padding" select="replace($padding_unreplaced, ' ', $padChar)"/>
        <xsl:choose>
            <xsl:when test="string-length($value) = $length">
                <xsl:sequence select="$value"/>
            </xsl:when>
            <xsl:when test="string-length($value) > $length">
                  <xsl:sequence select="substring($value, 1, $length)"/>
                <!--  <xsl:choose> 
                    <xsl:when test="$direction = $Direction_Left">
                        <xsl:sequence select="substring($value, 1, $length)"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:sequence
                            select="substring($value, string-length($value) - $length + 1, $length)"
                        />
                    </xsl:otherwise>
                </xsl:choose> -->
            </xsl:when>
            <xsl:when test="$direction = $Direction_Left">
                <xsl:sequence
                    select="concat(substring($padding, string-length($padding) - ($length - string-length($value)) + 1), $value)"
                />
            </xsl:when>
            <xsl:otherwise>
                <xsl:sequence
                    select="concat($value, substring($padding, string-length($padding) - ($length - string-length($value)) + 1))"
                />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>

    <xsl:function name="this:impliedDecimal">
        <xsl:param name="dollarAmount"/>
        <xsl:if test="$dollarAmount">
            <xsl:if test="not(contains($dollarAmount, '.'))">
                <xsl:value-of select="concat($dollarAmount, '00')"/>
            </xsl:if>
            <xsl:if test="contains($dollarAmount, '.')">
                <xsl:value-of
                    select="
                        if (string-length(substring-after($dollarAmount, '.')) = 1) then
                            (translate(concat($dollarAmount, '0'), '.', ''))
                        else
                            (translate($dollarAmount, '.', ''))"
                />
            </xsl:if>
            <xsl:if test="contains($dollarAmount, '-')">
                <xsl:if test="contains($dollarAmount, '+')">
                    <xsl:value-of select="translate(translate($dollarAmount, '-', '0'), '+', '-')"/>
                </xsl:if>
            </xsl:if>
        </xsl:if>
    </xsl:function>


</xsl:stylesheet>

I'm printing the records who have state code = 'IL' in the wd:EFW2_Year_End_Worker_State_Data node.

Current Output:

  1. Cris K Kodet Dallas Texas 1234
  2. Raj K Kodet Waco Texas 1243

Line is an Unnecessary Newline.

Expected Out Put:

  1. Cris K Kodet Dallas Texas 1234
  2. Raj K Kodet Waco Texas 1243

I know the new line is printing because of the Postion() != Last() condition. I'm looking to replace this logic, where i can avoid printing new line which are not necessary.

Thanks in advance !

Krish
  • 15
  • 6
  • **1.** Your input is not well-formed: `` does not have a closing tag. **2.** Next time, please reduce your example to the minimum required to demonstrate the problem - see: [mcve]. – michael.hor257k Apr 28 '17 at 07:24

2 Answers2

1

There are two ways I can see to solve your problem.

One.

Can you move Position()!=Last() condition just below xsl-foreach loop? This will ensure it does not print the number. However, there will be one extra new line which may not be harmful.

Two.

Use xsl if-else block. You will need to wrap the whole printing code inside if-else blocks. Pseudo code will be like this.

if(Position() = Last())
   Print all content without a new line
else
   Print all content with a new line

Check this post - How-to break a for-each loop in XSLT?

Community
  • 1
  • 1
murtazat
  • 399
  • 3
  • 12
1

I know the new line is printing because of the Postion() != Last() condition.

The new line is printing because you have selected all records to be processed, not just the ones from Illinois - and the last record processed is not from Illinois. As a result, the condition - which is applied only to records from Illinois - will never return true.

The solution is to select only records from Illinois - i.e. change:

<xsl:for-each select="wd:Response_Data/wd:EFW2_Year_End_Worker_Filing">

to:

<xsl:for-each select="wd:Response_Data/wd:EFW2_Year_End_Worker_Filing[wd:EFW2_Year_End_Worker_Filing_Data/wd:EFW2_Year_End_Worker_State_Data/wd:State = 'IL']">

and get rid of the xsl:if instruction checking for the state, along with its associated variable.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thank You So much Michael. That if conditioned worked like a charm and even the performance got improved now. Thank a lot for taking time and answering my question. Can you pls. suggest me the good references to improve my XSLT skill set. – Krish Apr 28 '17 at 15:18