Search code examples
xsltkeyxslt-1.0

copy nodes based on key values in previous node


I have the following input XML and want to copy the "Delivery" Stop elements:

<?xml version="1.0" encoding="UTF-8" ?>
<LeanXML>
    <ShipperLoadPlan>
        <LoadNumber>129516728</LoadNumber>
        <Stops>
            <Stop>
                <StopNumber>1</StopNumber>
                <StopType>Pickup</StopType>
                <Distance UOM="mi">0</Distance>
                <CalcDueDate TimeZone="EDT" TimeZoneDesc="America/New_York">09/27/2021
                    00:00</CalcDueDate>
                <ReferenceNums/>
                <OrderNums>
                    <OrderNum ShipperRef="6000002014_30SAP" ShipmentLegID="291"
                        ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                    <OrderNum ShipperRef="6000002014_10SAP" ShipmentLegID="2916"
                        ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                </OrderNums>
            </Stop>
            <Stop>
                <StopNumber>2</StopNumber>
                <StopType>Delivery</StopType>
                <Distance UOM="mi">0</Distance>
                <CalcDueDate TimeZone="CDT" TimeZoneDesc="America/Chicago">10/01/2021
                    00:00</CalcDueDate>
                <ReferenceNums/>
                <OrderNums>
                    <OrderNum ShipperRef="R6000002014_10SAP" ShipmentLegID="291608671"
                        ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                </OrderNums>
            </Stop>
            <Stop>
                <StopNumber>3</StopNumber>
                <StopType>Delivery</StopType>
                <Distance UOM="mi">473</Distance>
                <CalcDueDate TimeZone="CDT" TimeZoneDesc="America/Chicago">10/03/2021
                    00:00</CalcDueDate>
                <ReferenceNums/>
                <OrderNums>
                    <OrderNum ShipperRef="R6000002014_30SAP" ShipmentLegID="291634632"
                        ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                </OrderNums>
            </Stop>
        </Stops>
        <DivertedOrders/>
        <CompanyDefined/>
    </ShipperLoadPlan>
    
</LeanXML>

I came up with this XSLT using a Key I put on the StopType = Pickup:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xs xd"
    version="1.0">
    
    
    <xsl:key name="keyOrderLineNum" match="Stop[StopType='Pickup']/OrderNums" 
        use="concat(OrderNum/@ShipperRef,'|',../../CalcDueDate,'|',../../StopNumber)"/>
  
    <xsl:template match="@* | node()">
         <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
    
    
    <xsl:template match="Stop[StopType='Pickup']">
        
        <xsl:copy>
            <xsl:copy-of select="."/>
            <CopiedStop>
                <xsl:copy>
                    <xsl:value-of select="parent::Stops/Stop[StopType='Delivery']"/>
                </xsl:copy>
               
            </CopiedStop>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

But I am expecting this target XML, my bit question mark is how do I properly use my key to copy the Delivery Stop elements? Expected Target:

<?xml version="1.0" encoding="UTF-8" ?>
<LeanXML>
    <ShipperLoadPlan>
        <LoadNumber>129516728</LoadNumber>
        <Stops>
            <Stop>
                <StopNumber>1</StopNumber>
                <StopType>Pickup</StopType>
                <Distance UOM="mi">0</Distance>
                <CalcDueDate TimeZone="EDT" TimeZoneDesc="America/New_York">09/27/2021
                    00:00</CalcDueDate>
                <ReferenceNums/>
                <OrderNums>
                    <OrderNum ShipperRef="6000002014_30SAP" ShipmentLegID="291"
                        ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                    <OrderNum ShipperRef="6000002014_10SAP" ShipmentLegID="2916"
                        ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                </OrderNums>
                <CopiedStop>
                    <Stop>
                        <StopNumber>2</StopNumber>
                        <StopType>Delivery</StopType>
                        <Distance UOM="mi">0</Distance>
                        <CalcDueDate TimeZone="CDT" TimeZoneDesc="America/Chicago">10/01/2021
                            00:00</CalcDueDate>
                        <ReferenceNums/>
                        <OrderNums>
                            <OrderNum ShipperRef="R6000002014_10SAP" ShipmentLegID="291608671"
                                ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                        </OrderNums>
                    </Stop>
                    <Stop>
                        <StopNumber>3</StopNumber>
                        <StopType>Delivery</StopType>
                        <Distance UOM="mi">473</Distance>
                        <CalcDueDate TimeZone="CDT" TimeZoneDesc="America/Chicago">10/03/2021
                            00:00</CalcDueDate>
                        <ReferenceNums/>
                        <OrderNums>
                            <OrderNum ShipperRef="R6000002014_30SAP" ShipmentLegID="291634632"
                                ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                        </OrderNums>
                    </Stop>
                </CopiedStop>
            </Stop>
            <Stop>
                <StopNumber>2</StopNumber>
                <StopType>Delivery</StopType>
                <Distance UOM="mi">0</Distance>
                <CalcDueDate TimeZone="CDT" TimeZoneDesc="America/Chicago">10/01/2021
                    00:00</CalcDueDate>
                <ReferenceNums/>
                <OrderNums>
                    <OrderNum ShipperRef="R6000002014_10SAP" ShipmentLegID="291608671"
                        ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                </OrderNums>
            </Stop>
            <Stop>
                <StopNumber>3</StopNumber>
                <StopType>Delivery</StopType>
                <Distance UOM="mi">473</Distance>
                <CalcDueDate TimeZone="CDT" TimeZoneDesc="America/Chicago">10/03/2021
                    00:00</CalcDueDate>
                <ReferenceNums/>
                <OrderNums>
                    <OrderNum ShipperRef="R6000002014_30SAP" ShipmentLegID="291634632"
                        ShipmentLegSeq="1" ScheduleIntgKey="1">6000002014</OrderNum>
                </OrderNums>
            </Stop>
        </Stops>
        <DivertedOrders/>
        <CompanyDefined/>
    </ShipperLoadPlan>
    
</LeanXML>

Can anyone help me please achieve this? I appreciate any tips on this.


Solution

  • AFAICT, you want to do something like:

    XSLT 1.0

    <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="deiveryStops" match="Stop[StopType='Delivery']" use="OrderNums/OrderNum" />
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
          
    <xsl:template match="Stop[StopType='Pickup']">
        <xsl:copy>
            <xsl:copy-of select="*"/>
            <CopiedStop>
                <xsl:copy-of select="key('deiveryStops', OrderNums/OrderNum)"/>
            </CopiedStop>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    This ties the "Delivery" Stops to the "Pickup" Stop based on matching OrderNum alone.