xslt - 根据前一个节点中的键值复制节点
问题描述
我有以下输入 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>
谁能帮我实现这一目标?我很欣赏这方面的任何提示。
解决方案
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
。
推荐阅读
- opencv - 使用 Scipy 进行语义图像分割
- reactjs - 如何动态设置多个 Spring 实例?
- database - 在 Mongodb 上执行关系查询
- c# - 使用 .NET Core 在 Unix/Linux 环境中设置环境变量
- javascript - 使用 fetch 和 ES6 加载本地 JSON 文件
- jenkins - 如何根据容量跳过Jenkins中的阶段?
- h2o - 有没有办法从 H2O 绘制混淆矩阵?
- javascript - 括号符号对 document.querySelector 有效
- bash - 在bash中独立计算偶数和奇数行的平均值
- python - 给定另一个 numpy 数组,将 numpy.ndarray 的一些元素替换为零