XSL Transformation fails on JD Edwards report with poorly formed XML output

273 views Asked by At

I have a requirement to transform the output of a custom invoice program in JD Edwards for EDI. The XML data coming out of the report is poorly formed and I'm struggling to restructure the data to meet the EDI requirements. I am new to XSL, so I've been working through trial and error to arrive at the result.

One of the challenges that I face is that the report does not structure the data properly. The invoice line is not a proper descendant of the invoice header, as can be seen in the XML Input below. I've had the developer add the invoice number to each section in hopes that I may be able to restructure the data based on that invoice number.

My question is, how can I change the XSL stylesheet code so that the output matches my desired result, creating the proper parent/child/sibling structure?

Here is the XML Input:

<R5942015>
    <Main___F4211_F42119_S1>
        <Header_S2>
            <ShipDate_ID11>2016-04-06</ShipDate_ID11>
            <CustomerNumber_ID92>10779</CustomerNumber_ID92>
            <InvoiceDate_ID107>2016-04-07</InvoiceDate_ID107>
            <DealerNumber_ID6>44014104</DealerNumber_ID6>
            <ShipmentNumber_ID70>169638</ShipmentNumber_ID70>
            <InvoiceNumber_ID62>8729266</InvoiceNumber_ID62>
        </Header_S2>
    </Main___F4211_F42119_S1>
    <Main___F4211_F42119_S1>
        <Line_Info_S4>
            <AckLineNumber_D_ID1>13</AckLineNumber_D_ID1>
            <LineExtendedPrice_D_ID8>1230.00</LineExtendedPrice_D_ID8>
            <_nd_Item_Number_D_ID3>010799</_nd_Item_Number_D_ID3>
            <DisplayInvoiceNumber_ID42>8729266</DisplayInvoiceNumber_ID42>
            <LineQuantityShipped_D_ID35>2</LineQuantityShipped_D_ID35>
            <AckLine_Freight_ID21>54.78</AckLine_Freight_ID21>
        </Line_Info_S4>
        <Summary_Info_S8>
            <AckExtendedPrice_T_ID2>1230.00</AckExtendedPrice_T_ID2>
            <Freight_T_ID6>54.78</Freight_T_ID6>
            <GrandTotal_T_ID17>1284.78</GrandTotal_T_ID17>
            <DisplayInvoiceNumber_ID39>8729266</DisplayInvoiceNumber_ID39>
        </Summary_Info_S8>
        <Remit_To_S11>
            <Invoice_No__ID19>Invoice No:</Invoice_No__ID19>
            <DisplayInvoiceNumber_ID18>8729266</DisplayInvoiceNumber_ID18>
            <Service_Charge_Message_ID14>Service charge message...</Service_Charge_Message_ID14>
        </Remit_To_S11>
        <Header_S2>
            <ShipDate_ID11>2016-04-06</ShipDate_ID11>
            <CustomerNumber_ID92>11005</CustomerNumber_ID92>
            <InvoiceDate_ID107>2016-04-07</InvoiceDate_ID107>
            <DealerNumber_ID6>50857355</DealerNumber_ID6>
            <ShipmentNumber_ID70>169638</ShipmentNumber_ID70>
            <InvoiceNumber_ID62>8729267</InvoiceNumber_ID62>
        </Header_S2>
    </Main___F4211_F42119_S1>
    <Main___F4211_F42119_S1>
        <Line_Info_S4>
            <AckLineNumber_D_ID1>3</AckLineNumber_D_ID1>
            <LineExtendedPrice_D_ID8>665.00</LineExtendedPrice_D_ID8>
            <_nd_Item_Number_D_ID3>010512</_nd_Item_Number_D_ID3>
            <DisplayInvoiceNumber_ID42>8729267</DisplayInvoiceNumber_ID42>
            <LineQuantityShipped_D_ID35>1</LineQuantityShipped_D_ID35>
            <AckLine_Freight_ID21>22.64</AckLine_Freight_ID21>
        </Line_Info_S4>
    </Main___F4211_F42119_S1>
    <Main___F4211_F42119_S1>
        <Line_Info_S4>
            <AckLineNumber_D_ID1>4</AckLineNumber_D_ID1>
            <LineExtendedPrice_D_ID8>665.00</LineExtendedPrice_D_ID8>
            <_nd_Item_Number_D_ID3>010512</_nd_Item_Number_D_ID3>
            <DisplayInvoiceNumber_ID42>8729267</DisplayInvoiceNumber_ID42>
            <LineQuantityShipped_D_ID35>1</LineQuantityShipped_D_ID35>
            <AckLine_Freight_ID21>22.64</AckLine_Freight_ID21>
        </Line_Info_S4>
    </Main___F4211_F42119_S1>
    <Main___F4211_F42119_S1>
        <Line_Info_S4>
            <AckLineNumber_D_ID1>5</AckLineNumber_D_ID1>
            <LineExtendedPrice_D_ID8>715.00</LineExtendedPrice_D_ID8>
            <_nd_Item_Number_D_ID3>010512</_nd_Item_Number_D_ID3>
            <DisplayInvoiceNumber_ID42>8729267</DisplayInvoiceNumber_ID42>
            <LineQuantityShipped_D_ID35>1</LineQuantityShipped_D_ID35>
            <AckLine_Freight_ID21>22.64</AckLine_Freight_ID21>
        </Line_Info_S4>
    </Main___F4211_F42119_S1>
    <Main___F4211_F42119_S1>
        <Line_Info_S4>
            <AckLineNumber_D_ID1>11</AckLineNumber_D_ID1>
            <LineExtendedPrice_D_ID8>665.00</LineExtendedPrice_D_ID8>
            <_nd_Item_Number_D_ID3>010512</_nd_Item_Number_D_ID3>
            <DisplayInvoiceNumber_ID42>8729267</DisplayInvoiceNumber_ID42>
            <LineQuantityShipped_D_ID35>1</LineQuantityShipped_D_ID35>
            <AckLine_Freight_ID21>22.64</AckLine_Freight_ID21>
        </Line_Info_S4>
    </Main___F4211_F42119_S1>
    <Main___F4211_F42119_S1>
        <Line_Info_S4>
            <AckLineNumber_D_ID1>21</AckLineNumber_D_ID1>
            <LineExtendedPrice_D_ID8>630.00</LineExtendedPrice_D_ID8>
            <_nd_Item_Number_D_ID3>016799</_nd_Item_Number_D_ID3>
            <DisplayInvoiceNumber_ID42>8729267</DisplayInvoiceNumber_ID42>
            <LineQuantityShipped_D_ID35>1</LineQuantityShipped_D_ID35>
            <AckLine_Freight_ID21>22.64</AckLine_Freight_ID21>
        </Line_Info_S4>
    </Main___F4211_F42119_S1>
    <Main___F4211_F42119_S1>
        <Line_Info_S4>
            <AckLineNumber_D_ID1>23</AckLineNumber_D_ID1>
            <LineExtendedPrice_D_ID8>600.00</LineExtendedPrice_D_ID8>
            <_nd_Item_Number_D_ID3>010765</_nd_Item_Number_D_ID3>
            <DisplayInvoiceNumber_ID42>8729267</DisplayInvoiceNumber_ID42>
            <LineQuantityShipped_D_ID35>1</LineQuantityShipped_D_ID35>
            <AckLine_Freight_ID21>22.64</AckLine_Freight_ID21>
        </Line_Info_S4>
    </Main___F4211_F42119_S1>
    <Main___F4211_F42119_S1>
        <Line_Info_S4>
            <AckLineNumber_D_ID1>24</AckLineNumber_D_ID1>
            <LineExtendedPrice_D_ID8>715.00</LineExtendedPrice_D_ID8>
            <_nd_Item_Number_D_ID3>P10512</_nd_Item_Number_D_ID3>
            <DisplayInvoiceNumber_ID42>8729267</DisplayInvoiceNumber_ID42>
            <LineQuantityShipped_D_ID35>1</LineQuantityShipped_D_ID35>
            <AckLine_Freight_ID21>22.64</AckLine_Freight_ID21>
        </Line_Info_S4>
    </Main___F4211_F42119_S1>
    <Main___F4211_F42119_S1>
        <Line_Info_S4>
            <AckLineNumber_D_ID1>62</AckLineNumber_D_ID1>
            <LineExtendedPrice_D_ID8>630.00</LineExtendedPrice_D_ID8>
            <_nd_Item_Number_D_ID3>016799</_nd_Item_Number_D_ID3>
            <DisplayInvoiceNumber_ID42>8729267</DisplayInvoiceNumber_ID42>
            <LineQuantityShipped_D_ID35>1</LineQuantityShipped_D_ID35>
            <AckLine_Freight_ID21>22.64</AckLine_Freight_ID21>
        </Line_Info_S4>
        <Summary_Info_S8>
            <AckExtendedPrice_T_ID2>5285.00</AckExtendedPrice_T_ID2>
            <Freight_T_ID6>181.12</Freight_T_ID6>
            <GrandTotal_T_ID17>5466.12</GrandTotal_T_ID17>
            <DisplayInvoiceNumber_ID39>8729267</DisplayInvoiceNumber_ID39>
        </Summary_Info_S8>
        <Remit_To_S11>
            <Invoice_No__ID19>Invoice No:</Invoice_No__ID19>
            <DisplayInvoiceNumber_ID18>8729267</DisplayInvoiceNumber_ID18>
            <Service_Charge_Message_ID14>Service charge message...</Service_Charge_Message_ID14>
        </Remit_To_S11>
    </Main___F4211_F42119_S1>
</R5942015>

This is the desired output for EDI:

<R5942015>
    <Invoices>
        <InvNumber>8729266</InvNumber>
        <InvoiceDate>2016-04-07</InvoiceDate>
        <ShipmentNumber>169638</ShipmentNumber>
        <ShipDate>2016-04-06</ShipDate>
        <CustomerNumber>10779</CustomerNumber>
        <DealerNumber>44014104</DealerNumber>
        <line>
            <ITEMLineNumber>13</ITEMLineNumber>
            <ITEM2ndItemNumber>010799</ITEM2ndItemNumber>
            <QTYShipped>2</QTYShipped>
            <AMTLineExtendedPrice>1230.00</AMTLineExtendedPrice>
            <AMTLandedFreight>54.78</AMTLandedFreight>
        </line>
        <summary>
            <QTYOrdered>2</QTYOrdered>
            <QTYShipped>2</QTYShipped>
            <AMTLandedFreight>54.78</AMTLandedFreight>
            <AMTExtendedPrice>1230.00</AMTExtendedPrice>
            <AMTGrandTotal>1284.78</AMTGrandTotal>
        </summary>
        <messages>
            <MSGServiceCharge>Service charge message...</MSGServiceCharge>
        </messages>
    </Invoices>
    <Invoices>
        <InvNumber>8729267</InvNumber>
        <InvoiceDate>2016-04-07</InvoiceDate>
        <ShipmentNumber>169638</ShipmentNumber>
        <ShipDate>2016-04-06</ShipDate>
        <CustomerNumber>11005</CustomerNumber>
        <DealerNumber>50857355</DealerNumber>
        <line>
            <ITEMLineNumber>3</ITEMLineNumber>
            <ITEM2ndItemNumber>010512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>665.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>4</ITEMLineNumber>
            <ITEM2ndItemNumber>010512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>665.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>5</ITEMLineNumber>
            <ITEM2ndItemNumber>010512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>715.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>11</ITEMLineNumber>
            <ITEM2ndItemNumber>010512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>665.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>21</ITEMLineNumber>
            <ITEM2ndItemNumber>016799</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>630.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>23</ITEMLineNumber>
            <ITEM2ndItemNumber>010765</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>600.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>24</ITEMLineNumber>
            <ITEM2ndItemNumber>P10512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>715.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>62</ITEMLineNumber>
            <ITEM2ndItemNumber>016799</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>630.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <summary>
            <QTYOrdered>8</QTYOrdered>
            <QTYShipped>8</QTYShipped>
            <AMTLandedFreight>181.12</AMTLandedFreight>
            <AMTExtendedPrice>5285.00</AMTExtendedPrice>
            <AMTGrandTotal>5466.12</AMTGrandTotal>
        </summary>
        <messages>
            <MSGServiceCharge>Service charge message...</MSGServiceCharge>
        </messages>
    </Invoices>
</R5942015>

This is what I'm actually getting when the XSL Stylesheet runs. The detail line, summary and messages for the first invoice are actually displayed after the header for the second invoice.

<R5942015>
    <Invoices>
        <InvNumber>8729266</InvNumber>
        <InvoiceDate>2016-04-07</InvoiceDate>
        <ShipmentNumber>169638</ShipmentNumber>
        <ShipDate>2016-04-06</ShipDate>
        <CustomerNumber>10779</CustomerNumber>
        <DealerNumber>44014104</DealerNumber>
    </Invoices>
    <Invoices>
        <InvNumber>8729267</InvNumber>
        <InvoiceDate>2016-04-07</InvoiceDate>
        <ShipmentNumber>169638</ShipmentNumber>
        <ShipDate>2016-04-06</ShipDate>
        <CustomerNumber>11005</CustomerNumber>
        <DealerNumber>50857355</DealerNumber>
        <line>
            <ITEMLineNumber>13</ITEMLineNumber>
            <ITEM2ndItemNumber>010799</ITEM2ndItemNumber>
            <QTYShipped>2</QTYShipped>
            <AMTLineExtendedPrice>1230.00</AMTLineExtendedPrice>
        </line>
        <summary>
            <QTYOrdered>2</QTYOrdered>
            <QTYShipped>2</QTYShipped>
            <AMTExtendedPrice>1230.00</AMTExtendedPrice>
            <AMTGrandTotal>1284.78</AMTGrandTotal>
        </summary>
        <messages>
            <MSGServiceCharge>Service charge message...</MSGServiceCharge>
        </messages>
        <line>
            <ITEMLineNumber>3</ITEMLineNumber>
            <ITEM2ndItemNumber>010512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>665.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>4</ITEMLineNumber>
            <ITEM2ndItemNumber>010512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>665.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>5</ITEMLineNumber>
            <ITEM2ndItemNumber>010512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>715.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>11</ITEMLineNumber>
            <ITEM2ndItemNumber>010512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>665.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>21</ITEMLineNumber>
            <ITEM2ndItemNumber>016799</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>630.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>23</ITEMLineNumber>
            <ITEM2ndItemNumber>010765</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>600.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
        <line>
            <ITEMLineNumber>24</ITEMLineNumber>
            <ITEM2ndItemNumber>P10512</ITEM2ndItemNumber>
            <QTYShipped>1</QTYShipped>
            <AMTLineExtendedPrice>715.00</AMTLineExtendedPrice>
            <AMTLandedFreight>22.64</AMTLandedFreight>
        </line>
    </Invoices>
    <Invoices>
        <summary>
            <QTYOrdered>8</QTYOrdered>
            <QTYShipped>8</QTYShipped>
            <AMTExtendedPrice>5285.00</AMTExtendedPrice>
            <AMTGrandTotal>5466.12</AMTGrandTotal>
        </summary>
        <messages>
            <MSGServiceCharge>Service charge message...</MSGServiceCharge>
        </messages>
     </Invoices>  
</R5942015>    

This is what I've come up with for the stylesheet, trying to get around the poorly formed XML. Again, new to XSL, so please go easy:

<xsl:stylesheet version="2.0"
                xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
                xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
                xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ora="http://schemas.oracle.com/xpath/extension"
                xmlns:ehdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.esb.server.headers.ESBHeaderFunctions"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
                xmlns:orcl="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
                xmlns:ids="http://xmlns.oracle.com/bpel/services/IdentityService/xpath"
                xmlns:hwf="http://xmlns.oracle.com/bpel/workflow/xpath"
                exclude-result-prefixes="xsl xs msdata xref xp20 bpws ora ehdr orcl ids hwf">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/R5942015"> 
        <xsl:copy>
            <xsl:for-each-group select="Main___F4211_F42119_S1" group-starting-with="Main___F4211_F42119_S1[Header_S2]">
            <Invoices>
                    <xsl:apply-templates select="current-group()"/>
            </Invoices>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Main___F4211_F42119_S1[Header_S2]">
            <xsl:if test="Header_S2/InvoiceNumber_ID62"><InvNumber><xsl:value-of select="Header_S2/InvoiceNumber_ID62"/></InvNumber></xsl:if>
            <xsl:if test="Header_S2/InvoiceDate_ID107"><InvoiceDate><xsl:value-of select="Header_S2/InvoiceDate_ID107"/></InvoiceDate></xsl:if>
            <xsl:if test="Header_S2/ShipmentNumber_ID70"><ShipmentNumber><xsl:value-of select="Header_S2/ShipmentNumber_ID70"/></ShipmentNumber></xsl:if>
            <xsl:if test="Header_S2/ShipDate_ID11"><ShipDate><xsl:value-of select="Header_S2/ShipDate_ID11"/></ShipDate></xsl:if>
            <xsl:if test="Header_S2/CustomerNumber_ID92"><CustomerNumber><xsl:value-of select="Header_S2/CustomerNumber_ID92"/></CustomerNumber></xsl:if>
            <xsl:if test="Header_S2/DealerNumber_ID6"><DealerNumber><xsl:value-of select="Header_S2/DealerNumber_ID6"/></DealerNumber></xsl:if>
            <xsl:if test="Line_Info_S4"><line>
                <xsl:if test="Line_Info_S4/AckLineNumber_D_ID1"><ITEMLineNumber><xsl:value-of select="Line_Info_S4/AckLineNumber_D_ID1"/></ITEMLineNumber></xsl:if>
                <xsl:if test="Line_Info_S4/_nd_Item_Number_D_ID3"><ITEM2ndItemNumber><xsl:value-of select="Line_Info_S4/_nd_Item_Number_D_ID3"/></ITEM2ndItemNumber></xsl:if>
                <xsl:if test="Line_Info_S4/LineQuantityShipped_D_ID35"><QTYShipped><xsl:value-of select="Line_Info_S4/LineQuantityShipped_D_ID35"/></QTYShipped></xsl:if>
                <xsl:if test="Line_Info_S4/LineExtendedPrice_D_ID8"><AMTLineExtendedPrice><xsl:value-of select="Line_Info_S4/LineExtendedPrice_D_ID8"/></AMTLineExtendedPrice></xsl:if>
                <xsl:if test="Line_Info_S4/AckLine_Freight_ID21"><AMTLandedFreight><xsl:value-of select="Line_Info_S4/AckLine_Freight_ID21"/></AMTLandedFreight></xsl:if>
            </line></xsl:if>
            <xsl:if test="Summary_Info_S8"><summary>
                <xsl:if test="Summary_Info_S8/AckQuantity_T_ID10"><QTYOrdered><xsl:value-of select="Summary_Info_S8/AckQuantity_T_ID10"/></QTYOrdered></xsl:if>
                <xsl:if test="Summary_Info_S8/QuantityShipped_T_ID13"><QTYShipped><xsl:value-of select="Summary_Info_S8/QuantityShipped_T_ID13"/></QTYShipped></xsl:if>
                <xsl:if test="Summary_Info_S8/LandedFreight_T_ID6"><AMTLandedFreight><xsl:value-of select="Summary_Info_S8/LandedFreight_T_ID6"/></AMTLandedFreight></xsl:if>
                <xsl:if test="Summary_Info_S8/AckExtendedPrice_T_ID2"><AMTExtendedPrice><xsl:value-of select="Summary_Info_S8/AckExtendedPrice_T_ID2"/></AMTExtendedPrice></xsl:if>
                <xsl:if test="Summary_Info_S8/GrandTotal_T_ID17"><AMTGrandTotal><xsl:value-of select="Summary_Info_S8/GrandTotal_T_ID17"/></AMTGrandTotal></xsl:if>
            </summary></xsl:if>
            <xsl:if test="Remit_To_S11"><messages>
                <xsl:if test="Remit_To_S11/Service_Charge_Message_ID14"><MSGServiceCharge><xsl:value-of select="Remit_To_S11/Service_Charge_Message_ID14"/></MSGServiceCharge></xsl:if>
            </messages></xsl:if>
    </xsl:template>

    <xsl:template match="Main___F4211_F42119_S1">
            <xsl:if test="Line_Info_S4"><line>
                <xsl:if test="Line_Info_S4/AckLineNumber_D_ID1"><ITEMLineNumber><xsl:value-of select="Line_Info_S4/AckLineNumber_D_ID1"/></ITEMLineNumber></xsl:if>
                <xsl:if test="Line_Info_S4/_nd_Item_Number_D_ID3"><ITEM2ndItemNumber><xsl:value-of select="Line_Info_S4/_nd_Item_Number_D_ID3"/></ITEM2ndItemNumber></xsl:if>
                <xsl:if test="Line_Info_S4/LineQuantityOrdered_D_ID10"><QTYOrdered><xsl:value-of select="Line_Info_S4/LineQuantityOrdered_D_ID10"/></QTYOrdered></xsl:if>
                <xsl:if test="Line_Info_S4/LineQuantityShipped_D_ID35"><QTYShipped><xsl:value-of select="Line_Info_S4/LineQuantityShipped_D_ID35"/></QTYShipped></xsl:if>
                <xsl:if test="Line_Info_S4/LineExtendedPrice_D_ID8"><AMTLineExtendedPrice><xsl:value-of select="Line_Info_S4/LineExtendedPrice_D_ID8"/></AMTLineExtendedPrice></xsl:if>
                <xsl:if test="Line_Info_S4/AckLine_Freight_ID21"><AMTLandedFreight><xsl:value-of select="Line_Info_S4/AckLine_Freight_ID21"/></AMTLandedFreight></xsl:if>
            </line></xsl:if>
            <xsl:if test="Summary_Info_S8"><summary>
                <xsl:if test="Summary_Info_S8/AckQuantity_T_ID10"><QTYOrdered><xsl:value-of select="Summary_Info_S8/AckQuantity_T_ID10"/></QTYOrdered></xsl:if>
                <xsl:if test="Summary_Info_S8/QuantityShipped_T_ID13"><QTYShipped><xsl:value-of select="Summary_Info_S8/QuantityShipped_T_ID13"/></QTYShipped></xsl:if>
                <xsl:if test="Summary_Info_S8/LandedFreight_T_ID6"><AMTLandedFreight><xsl:value-of select="Summary_Info_S8/LandedFreight_T_ID6"/></AMTLandedFreight></xsl:if>
                <xsl:if test="Summary_Info_S8/AckExtendedPrice_T_ID2"><AMTExtendedPrice><xsl:value-of select="Summary_Info_S8/AckExtendedPrice_T_ID2"/></AMTExtendedPrice></xsl:if>
                <xsl:if test="Summary_Info_S8/GrandTotal_T_ID17"><AMTGrandTotal><xsl:value-of select="Summary_Info_S8/GrandTotal_T_ID17"/></AMTGrandTotal></xsl:if>
            </summary></xsl:if>
            <xsl:if test="Header_S2">
            <xsl:if test="Header_S2/InvoiceNumber_ID62"><InvNumber><xsl:value-of select="Header_S2/InvoiceNumber_ID62"/></InvNumber></xsl:if>
            <xsl:if test="Header_S2/InvoiceDate_ID107"><InvoiceDate><xsl:value-of select="Header_S2/InvoiceDate_ID107"/></InvoiceDate></xsl:if>
            <xsl:if test="Header_S2/ShipmentNumber_ID70"><ShipmentNumber><xsl:value-of select="Header_S2/ShipmentNumber_ID70"/></ShipmentNumber></xsl:if>
            <xsl:if test="Header_S2/ShipDate_ID11"><ShipDate><xsl:value-of select="Header_S2/ShipDate_ID11"/></ShipDate></xsl:if>
            <xsl:if test="Header_S2/CustomerNumber_ID92"><CustomerNumber><xsl:value-of select="Header_S2/CustomerNumber_ID92"/></CustomerNumber></xsl:if>
            <xsl:if test="Header_S2/DealerNumber_ID6"><DealerNumber><xsl:value-of select="Header_S2/DealerNumber_ID6"/></DealerNumber></xsl:if>
            <xsl:if test="Remit_To_S11"><messages>
                <xsl:if test="Remit_To_S11/Service_Charge_Message_ID14"><MSGServiceCharge><xsl:value-of select="Remit_To_S11/Service_Charge_Message_ID14"/></MSGServiceCharge></xsl:if>
            </messages></xsl:if>
    </xsl:template>
</xsl:stylesheet>

Thank you for reviewing this question. If you saw my previous question about this same topic, I apologize for the poor structure of the question.

1

There are 1 answers

1
michael.hor257k On BEST ANSWER

I could not follow the logic of your attempted XSLT. I would suggest you try a different approach - here's a skeleton example:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="line" match="Line_Info_S4" use="DisplayInvoiceNumber_ID42" />
<xsl:key name="summary" match="Summary_Info_S8" use="DisplayInvoiceNumber_ID39" />
<xsl:key name="remit" match="Remit_To_S11" use="DisplayInvoiceNumber_ID18" />

<xsl:template match="/R5942015">
    <xsl:copy>
        <xsl:apply-templates select="Main___F4211_F42119_S1/Header_S2"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Header_S2">
    <Invoices>
        <InvNumber>
            <xsl:value-of select="InvoiceNumber_ID62" />
        </InvNumber>
        <!-- more fields here -->
        <xsl:apply-templates select="key('line', InvoiceNumber_ID62)"/>
        <xsl:apply-templates select="key('summary', InvoiceNumber_ID62)"/>
        <xsl:apply-templates select="key('remit', InvoiceNumber_ID62)"/>
    </Invoices>
</xsl:template>

<xsl:template match="Line_Info_S4">
    <line>
        <ITEMLineNumber>
            <xsl:value-of select="AckLineNumber_D_ID1" />
        </ITEMLineNumber>
        <!-- more fields here -->
    </line>
</xsl:template>

<xsl:template match="Summary_Info_S8">
    <summary>
        <!-- more fields here -->
        <AMTGrandTotal>
            <xsl:value-of select="GrandTotal_T_ID17" />
        </AMTGrandTotal>
    </summary>
</xsl:template>

<xsl:template match="Remit_To_S11">
    <messages>
        <MSGServiceCharge>
            <xsl:value-of select="Service_Charge_Message_ID14" />
        </MSGServiceCharge>
    </messages>
</xsl:template>

</xsl:stylesheet>

I am assuming here that the names of the elements are consistent and will not change arbitrarily from one input to another.