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

XSLT 1.0去重求助:新手寻求可行解决方案

Remove Duplicate OrderNumber Entries with XSLT 1.0

Hey there! I see you're new to XSLT and stuck removing duplicate OrderNumber entries in your XML—let's fix that. Since you're limited to XSLT 1.0, we'll use the Muenchian Method (the standard go-to approach for grouping and deduplication in 1.0).

First, let's clean up your input XML (it had a misplaced closing </VariableCollection> tag; here's the corrected version):

<?xml version="1.0" encoding="UTF-8"?>
<OrderNumberVar> 
  <VariableCollection xmlns="http://www.mcp.com/xsd" xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:client="http://xmlns.oracle.com/MCB_SOA/JDE" xmlns:tns="http://www.mcb.com/xsd"> 
    <tns:Variable> 
      <tns:OrderNumber>156708</tns:OrderNumber> 
    </tns:Variable> 
    <tns:Variable> 
      <tns:OrderNumber>156708</tns:OrderNumber> 
    </tns:Variable> 
    <tns:Variable> 
      <tns:OrderNumber>263932</tns:OrderNumber> 
    </tns:Variable> 
  </VariableCollection>
</OrderNumberVar>

XSLT 1.0 Solution

This stylesheet will group <tns:Variable> nodes by their <tns:OrderNumber> value and keep only one instance of each:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tns="http://www.mcb.com/xsd"
    xmlns="http://www.mcp.com/xsd">

    <!-- Define a key to group Variable nodes by their OrderNumber value -->
    <xsl:key name="orderNumberKey" match="tns:Variable" use="tns:OrderNumber"/>

    <!-- Copy all nodes/attributes by default to preserve XML structure -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Remove duplicate Variable nodes: only keep the first node in each OrderNumber group -->
    <xsl:template match="tns:Variable[not(generate-id() = generate-id(key('orderNumberKey', tns:OrderNumber)[1]))]"/>

</xsl:stylesheet>

How It Works

  • Key Definition: The <xsl:key> creates an index of all <tns:Variable> nodes, grouped by the text inside <tns:OrderNumber>. This lets us easily find all duplicates of a specific order number.
  • Identity Template: This template copies every node and attribute by default, so your original XML structure stays intact except for the duplicates we want to remove.
  • Duplicate Removal Template: This template targets any <tns:Variable> that isn't the first node in its OrderNumber group (checked using generate-id() to compare unique identifiers). Matching these nodes and doing nothing with them effectively removes them from the output.

Result After Transformation

You'll get this cleaned-up XML with unique OrderNumber entries:

<?xml version="1.0" encoding="UTF-8"?>
<OrderNumberVar> 
  <VariableCollection xmlns="http://www.mcp.com/xsd" xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:client="http://xmlns.oracle.com/MCB_SOA/JDE" xmlns:tns="http://www.mcb.com/xsd"> 
    <tns:Variable> 
      <tns:OrderNumber>156708</tns:OrderNumber> 
    </tns:Variable> 
    <tns:Variable> 
      <tns:OrderNumber>263932</tns:OrderNumber> 
    </tns:Variable> 
  </VariableCollection>
</OrderNumberVar>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:29:23