【问题标题】:MAX Value & its Corresponding Nodes in XSLTXSLT 中的 MAX 值及其对应节点
【发布时间】:2018-05-16 23:39:07
【问题描述】:

我有点卡在从简单的结构化 XML 中获取 Qty 的最大值。所以需要一些帮助!

我的 XML 如下:

  <?xml version="1.0" encoding="utf-8"?>
<TANK_DETAILS>
    <TABLES>
        <OUTPUT>
            <item>
                <AREA>INDIA</AREA>
                <MACHINE>T100</MACHINE>
                <MATERIAL>1111111</MATERIAL>
                <BATCH>100</BATCH>
                <QTY>18815.000</QTY>
            </item>
            <item>
                <AREA>INDIA</AREA>
                <MACHINE>T102</MACHINE>
                <MATERIAL>1111111</MATERIAL>
                <BATCH>200</BATCH>
                <QTY>100.000</QTY>
            </item>
            <item>
                <AREA>INDIA</AREA>
                <MACHINE>T103</MACHINE>
                <MATERIAL>1111111</MATERIAL>
                <BATCH>300</BATCH>
                <QTY>10000.000</QTY>
            </item>
            <item>
                <AREA>INDIA</AREA>
                <MACHINE>T101</MACHINE>
                <MATERIAL>1111111</MATERIAL>
                <BATCH>400</BATCH>
                <QTY>35550.000</QTY>
            </item>
            <item>
                <AREA>INDIA</AREA>
                <MACHINE>T104</MACHINE>
                <MATERIAL>1111111</MATERIAL>
                <BATCH>500</BATCH>
                <QTY>10100.000</QTY>
            </item>
            </OUTPUT>
            </TABLES>
        </TANK_DETAILS>

我想得到&lt;MACHINE&gt;,&lt;BATCH&gt; &amp; &lt;Qty&gt; 其中&lt;QTY&gt; 是最大值。

输出 XML 应如下所示:

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

我正在尝试的 XSLT 如下:

 <?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="*"/>

    <!-- Putting the maximum QTY from the list into a variable -->
    <xsl:variable name="max-QTY">
      <xsl:call-template name="find-max">

        <!-- Select the list of QTY elements -->
        <xsl:with-param name="QTY" select="//*[contains(local-name(), 'QTY')]"/>
      </xsl:call-template>
    </xsl:variable>


    <xsl:template match="*">
        <!-- Displaying the result -->
        <QTY>
            <xsl:value-of select="$max-QTY"/>
        </QTY>
    </xsl:template>


    <!-- This template works recursively on the list of QTY. -->
    <xsl:template name="find-max">
        <xsl:param name="QTY"/>
        <!-- The value of the first QTY in this list. -->
        <xsl:variable name="this-QTY">
      <xsl:value-of select="$QTY[position() = 1]"/>
    </xsl:variable>

        <xsl:choose>
            <xsl:when test="$QTY">
                <!-- The maximum value of the remaining QTY in this list. -->
                <xsl:variable name="other-QTY">
          <xsl:call-template name="find-max">
            <xsl:with-param name="QTY" select="$QTY[position() != 1]"/>
          </xsl:call-template>
        </xsl:variable>
                <!-- Return the maximum of this QTYand the remaining QTY. -->
                <xsl:choose>
                    <xsl:when test="$other-QTY&gt; $this-QTY">
                        <xsl:value-of select="$other-QTY"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$this-QTY"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <!-- We've reached the last QTY in the list. -->
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

我得到的输出是:

<?xml version="1.0" encoding="utf-8"?>
<QTY>35550.000</QTY>

所以我能够找到最大 &lt;QTY&gt;,但我正在努力为最大 &lt;QTY&gt; 获得相应的 &lt;MACHINE&gt; &amp; &lt;BATCH&gt;

我确定我犯了一个小错误,但我非常卡住并面临编码人员的障碍。

另外,如果有更好的方法来找到 max ,请提供最佳答案。那会很有帮助。

谢谢。

【问题讨论】:

    标签: xml xslt xslt-1.0 xslt-2.0


    【解决方案1】:

    我只需将items 按QTY 排序,然后输出第一个(按降序排列)或最后一个(按升序排列):

    <xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
    
      <xsl:output method="xml" indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="TABLES">
          <Rowsets>
              <Rowset>
                  <xsl:for-each select="OUTPUT/item">
                      <xsl:sort select="QTY" data-type="number" order="descending"/>
                      <xsl:if test="position() = 1">
                          <Row>
                              <xsl:copy-of select="MACHINE | BATCH | QTY"/>
                          </Row>
                      </xsl:if>
                  </xsl:for-each>
              </Rowset>
          </Rowsets>
      </xsl:template>
    
    </xsl:stylesheet>
    

    https://xsltfiddle.liberty-development.net/94hvTz2 有一个在线示例。

    【讨论】:

    • 这就像一个魅力。我不知道这很容易实现。你只需要知道语法 :) 非常感谢!!!
    【解决方案2】:

    我会从一个钥匙开始,比如:

    <xsl:key name="kQTY" match="QTY" use="."/>
    

    然后,我会将最大数量存储在一个变量中,例如:

    <xsl:variable name="maxQTY">
        <xsl:for-each select="//item">
            <xsl:sort select="key('kQTY', QTY)" order="descending"/>
            <xsl:if test="position()=1">
                <xsl:value-of select="QTY"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>
    

    然后,在匹配OUTPUT节点的模板中,我会过滤掉包含最大QTY的item,比如:

    <xsl:template match="OUTPUT">
        <Rowset>
                <xsl:apply-templates select="item[QTY=$maxQTY]"/>
        </Rowset>
    </xsl:template>
    

    然后需要一个模板来过滤项目内容:

    <xsl:template match="item">
        <xsl:apply-templates select="MACHINE,BATCH,QTY"/>
    </xsl:template>
    

    一些匹配的清理模板

    <xsl:template match="TANK_DETAILS">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="TABLES">
        <Rowsets>
            <xsl:apply-templates/>
        </Rowsets>
    </xsl:template>
    

    和一个身份模板:

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    

    整个样式表如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs"
        version="2.0">
    
        <xsl:strip-space elements="*"/>
        <xsl:output indent="yes" omit-xml-declaration="yes"/>
    
        <xsl:key name="kQTY" match="QTY" use="."/>
    
        <xsl:variable name="maxQTY">
            <xsl:for-each select="//item">
                <xsl:sort select="key('kQTY', QTY)" order="descending"/>
                <xsl:if test="position()=1">
                    <xsl:value-of select="QTY"/>
                </xsl:if>
            </xsl:for-each>
        </xsl:variable>
    
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="OUTPUT">
            <Rowset>
                    <xsl:apply-templates select="item[QTY=$maxQTY]"/>
            </Rowset>
        </xsl:template>
    
        <xsl:template match="item">
            <xsl:apply-templates select="MACHINE,BATCH,QTY"/>
        </xsl:template>
    
        <xsl:template match="TANK_DETAILS">
            <xsl:apply-templates/>
        </xsl:template>
    
        <xsl:template match="TABLES">
            <Rowsets>
                <xsl:apply-templates/>
            </Rowsets>
        </xsl:template>
    
    </xsl:stylesheet> 
    

    看到它在行动here

    【讨论】:

    • 我喜欢你的详细解释。但是,如果您看到@Martin Honnen 的回答,它清晰明了,而且看起来更理想。请检查他的答案!
    • 由于我不知道您的样式表的全部范围,我只是按照您的指示获得了最大数量。我只是假设你会在其他地方使用它。马丁的回答很直接,是的。
    • 我完全同意。如果我稍后需要在 XSLT 的某个地方使用 max qty,您的回答也很有用。非常感谢您的努力。
    猜你喜欢
    • 1970-01-01
    • 2021-09-05
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多