You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSLT开发:仅子节点无PAL类型Identification时复制父容器编码

解决XSLT条件复制Identification节点的问题

核心需求是:仅当ContainmentLine/containedItemIdentification下不存在IdentificationType="PAL"的<Identification>标签时,才将上层LogisticUnitLineItem/logisticUnitIdentification/ContainerCode/serialShippingContainerCode的值作为PAL类型的<Identification>节点添加进去。

示例输入

<LogisticUnitLineItem>
  <logisticUnitIdentification>
    <ContainerCode>
      <serialShippingContainerCode>PAL12345</serialShippingContainerCode>
    </ContainerCode>
  </logisticUnitIdentification>
  <ContainmentLine>
    <containedItemIdentification>
      <Identification IdentificationType="BOX">BOX001</Identification>
    </containedItemIdentification>
  </ContainmentLine>
  <ContainmentLine>
    <containedItemIdentification>
      <Identification IdentificationType="PAL">PAL67890</Identification>
    </containedItemIdentification>
  </ContainmentLine>
</LogisticUnitLineItem>

期望输出

<LogisticUnitLineItem>
  <logisticUnitIdentification>
    <ContainerCode>
      <serialShippingContainerCode>PAL12345</serialShippingContainerCode>
    </ContainerCode>
  </logisticUnitIdentification>
  <ContainmentLine>
    <containedItemIdentification>
      <Identification IdentificationType="BOX">BOX001</Identification>
      <Identification IdentificationType="PAL">PAL12345</Identification>
    </containedItemIdentification>
  </ContainmentLine>
  <ContainmentLine>
    <containedItemIdentification>
      <Identification IdentificationType="PAL">PAL67890</Identification>
    </containedItemIdentification>
  </ContainmentLine>
</LogisticUnitLineItem>

修改后的XSLT代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- 默认模板:原样复制所有节点和属性,保证原有内容不受影响 -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- 专门处理containedItemIdentification节点,添加条件判断逻辑 -->
  <xsl:template match="containedItemIdentification">
    <xsl:copy>
      <!-- 先复制节点原有的所有子内容 -->
      <xsl:apply-templates select="@*|node()"/>
      <!-- 条件:仅当当前节点下没有IdentificationType为PAL的Identification时才执行添加 -->
      <xsl:if test="not(Identification[@IdentificationType='PAL'])">
        <Identification IdentificationType="PAL">
          <!-- 向上遍历获取上层的serialShippingContainerCode值 -->
          <xsl:value-of select="../../logisticUnitIdentification/ContainerCode/serialShippingContainerCode"/>
        </Identification>
      </xsl:if>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

关键说明

  • 默认模板确保原有XML结构和内容完全保留,只针对目标节点做修改。
  • 通过not(Identification[@IdentificationType='PAL'])精准判断是否缺少PAL类型的标识节点,避免重复添加。
  • 使用相对路径../../logisticUnitIdentification/ContainerCode/serialShippingContainerCode定位到上层的目标值,确保路径匹配你的XML层级结构。

内容的提问来源于stack exchange,提问作者CHAR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 07:00:08