根据前一个节点中的键值复制节点

copy nodes based on key values in previous node

我有以下输入 XML 并想复制“交付”Stop 元素:

<?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>

我使用放在 StopType = Pickup:

上的密钥想出了这个 XSLT
<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>

但我期待这个目标 XML,我的问题是如何正确使用我的密钥来复制 Delivery Stop 元素? 预期目标:

<?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>

谁能帮我实现这个目标?我很感激有关此的任何提示。

AFAICT,您想执行以下操作:

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>

这仅基于匹配 OrderNum 将“送货”站点与“取货”站点联系起来。