首页 > 解决方案 > 根据前一个节点中的键值复制节点

问题描述

我有以下输入 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 上的 Key 提出了这个 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>

谁能帮我实现这一目标?我很欣赏这方面的任何提示。

标签: xsltkeyxslt-1.0

解决方案


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


推荐阅读