MAX Value&它在XSLT中的对应节点

时间:2018-05-16 23:39:08

标签: xml xslt xslt-1.0 xslt-2.0

我从一个简单的结构化XML中获取Max值的数量有点困难。所以需要一些帮助!

我的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>

我希望得到<MACHINE>,<BATCH> & <Qty> <QTY>最多的地方。

输出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>

输出I&#39;得到的是:

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

所以我能够找到最大<QTY>,但我很难获得最大<MACHINE> & <BATCH>的{​​{1}}。

我确定我犯的是一个小错误,但我非常困难并面对一个程序员&#39;块。

此外,如果有更好的方法来查找最大值,请提供最佳答案。这会有所帮助。

感谢。

2 个答案:

答案 0 :(得分:1)

我会通过使用密钥来启动它,例如:

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

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

<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节点匹配的模板中,我会过滤包含最大数量的项目,例如:

<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中查看。

答案 1 :(得分:1)

我只需按itemQTY进行排序,然后输出第一个(按降序排序)或最后一个(按升序排序):

<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有一个在线样本。