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

使用XSLT提取每个userId对应的最高collectorValue记录

提取每个用户最高collectorValue记录的XSLT实现

由于你的源XML已经按userId升序、collectorValue降序完成排序,每个用户的最高collectorValue记录就是该用户分组下的第一条记录。以下是两种实用的实现方式:

方法1:分组筛选(XSLT 2.0及以上)

利用xsl:for-each-group按userId分组,直接选取每组的第一个节点:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <!-- 匹配根节点 -->
  <xsl:template match="/TimeCollector">
    <xsl:copy>
      <!-- 按userId分组 -->
      <xsl:for-each-group select="TimeCollector" group-by="userId">
        <!-- 输出每组第一条记录(即当前用户最高collectorValue记录) -->
        <xsl:copy-of select="current-group()[1]"/>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

方法2:节点位置判断(兼容XSLT 1.0)

如果只能使用XSLT 1.0,可通过判断当前节点的前序节点是否有相同userId来筛选:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <!-- 匹配根节点 -->
  <xsl:template match="/TimeCollector">
    <xsl:copy>
      <!-- 筛选出每个userId的第一条记录 -->
      <xsl:apply-templates select="TimeCollector[not(userId = preceding-sibling::TimeCollector/userId)]"/>
    </xsl:copy>
  </xsl:template>

  <!-- 复制匹配到的节点及其子节点 -->
  <xsl:template match="TimeCollector">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>

转换结果

两种方法都会输出你预期的目标XML:

<TimeCollector>
  <TimeCollector>
    <collectorValue>4</collectorValue>
    <userId>UAT1101767</userId>
    <startDate>2023-02-01T00:00:00.000</startDate>
  </TimeCollector>
  <TimeCollector>
    <collectorValue>3</collectorValue>
    <userId>UAT1103429</userId>
    <startDate>2023-02-01T00:00:00.000</startDate>
  </TimeCollector>
  <TimeCollector>
    <collectorValue>5</collectorValue>
    <userId>UAT1103746</userId>
    <startDate>2023-02-01T00:00:00.000</startDate>
  </TimeCollector>
</TimeCollector>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 05:55:15