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

如何在XSLT中获取QTY最大值及其对应节点?

Solution for Fetching Max QTY with Associated Machine & Batch Details

Hey there! You're already halfway there by getting the maximum QTY value. The key missing piece is linking that value back to its parent <item> element so we can pull in the related MACHINE and BATCH data. Plus, we can simplify the max-finding logic to avoid the recursive template overhead in XSLT 1.0.

The Core Issue

Your current code calculates the max QTY but doesn't track which <item> it belongs to. Instead of isolating the QTY value first, we should directly select the <item> that has the largest QTY, then extract all the fields we need from it.

Optimized XSLT Code

Here's a streamlined solution that gets you exactly the output you want:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- Match the root node to start processing -->
    <xsl:template match="/">
        <Rowsets>
            <Rowset>
                <!-- Select the item(s) with the maximum QTY -->
                <!-- Use [1] at the end if you only want the first max item in case of ties -->
                <xsl:for-each select="//item[not(../item/QTY > QTY)]">
                    <Row>
                        <MACHINE><xsl:value-of select="MACHINE"/></MACHINE>
                        <BATCH><xsl:value-of select="BATCH"/></BATCH>
                        <QTY><xsl:value-of select="QTY"/></QTY>
                    </Row>
                </xsl:for-each>
            </Rowset>
        </Rowsets>
    </xsl:template>
</xsl:stylesheet>

How This Works

  • Selecting the Max QTY Item: The expression //item[not(../item/QTY > QTY)] finds all <item> elements where no other <item> in the same parent has a larger QTY value. This is a clean, efficient way to get max values in XSLT 1.0 without recursion.
  • Generating the Output: We wrap the selected item's MACHINE, BATCH, and QTY in the exact XML structure you requested (Rowsets → Rowset → Row).
  • Handling Ties: If multiple items have the same maximum QTY, this code will output a <Row> for each. If you only want the first one, add [1] to the select expression like this: //item[not(../item/QTY > QTY)][1].

Expected Output

When you run this XSLT against your input XML, you'll get:

<Rowsets>
  <Rowset>
    <Row>
      <MACHINE>T101</MACHINE>
      <BATCH>400</BATCH>
      <QTY>35550.000</QTY>
    </Row>
  </Rowset>
</Rowsets>

Why This Is Better Than Your Original Code

  • No need for a recursive find-max template—this approach is more readable and efficient.
  • Directly links the max QTY to its source <item> so we can retrieve all associated fields in one step.
  • Easy to adjust if you need to handle edge cases like multiple max values.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:20:40