简体   繁体   English

如何在XSLT1.0中按不同类别查找最大价值组

[英]how to find the largest value group by different categories in XSLT1.0

I am using XSLT version 1 to transform the XML, the rule is to find the largest quantity in two countries respectively, and the xml structure is a little complex, what functions should I use? 我正在使用XSLT版本1转换XML,规则是分别在两个国家/地区找到最大的数量,并且xml结构有点复杂,我应该使用哪些功能?

I already used the generate-id() and sort function to 1, group by the country 2, sort the value of the quantity, 3, select the position = 1 我已经使用了generate-id()并将sort函数设置为1,按国家/地区分组2,对数量的值进行排序,3,选择位置= 1

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="bycountry" match="warehouses/warehouse/address" use="w_country" />
  <xsl:template match="/">
  <html>
<body>
<table border="1">
      <tr>
      <th>Warehouse-country</th>
      <th>name</th>
      <th>Quantity</th>
       </tr>
       <xsl:for-each select="warehouses/warehouse[generate-id()=generate-id(key('bycountry',w_country))]">
         <xsl:for-each select="items/item/s_qty">
            <xsl:sort select="." order="descending"/>
            <xsl:if test="position()=1">
       <tr>
            <td><xsl:value-of select="address/w_country"/></td>
          <td><xsl:value-of select="w_name"/></td>
          <td><xsl:value-of select="items/item/s_qty"/></td>
    </tr>
  </xsl:if>
  </xsl:for-each>
  </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<warehouses>
    <warehouse>
             <w_id>22</w_id>
             <w_name>Namekagon</w_name>
             <address>
                    <w_street>Anniversary</w_street>
                    <w_city>Singapore</w_city>
                    <w_country>Singapore</w_country>
             </address>
             <items>
                   <item>
                        <i_id>4</i_id>
                        <i_im_id>54868007</i_im_id>
                        <i_name>MECLIZINE HYDROCHLORIDE</i_name>
                        <i_price>54.49</i_price>
                        <s_qty>909</s_qty>
                    </item>
                </items>
      </warehouse>
      <warehouse>
             <w_id>21</w_id>
             <w_name>kagon</w_name>
             <address>
                    <w_street>Anniver</w_street>
                    <w_city>Singapore</w_city>
                    <w_country>Singapore</w_country>
             </address>
             <items>
                   <item>
                        <i_id>4</i_id>
                        <i_im_id>54868007</i_im_id>
                        <i_name>MECLIZINE HYDROCHLORIDE</i_name>
                        <i_price>54.49</i_price>
                        <s_qty>587</s_qty>
                    </item>
                </items>
      </warehouse>
      <warehouse>
             <w_id>21</w_id>
             <w_name>kagon</w_name>
             <address>
                    <w_street>Anniver</w_street>
                    <w_city>Kuala Lumpur</w_city>
                    <w_country>Malaysia</w_country>
             </address>
             <items>
                   <item>
                        <i_id>4</i_id>
                        <i_im_id>54868007</i_im_id>
                        <i_name>MECLIZINE HYDROCHLORIDE</i_name>
                        <i_price>54.49</i_price>
                        <s_qty>789</s_qty>
                    </item>
                </items>
      </warehouse>
</warehouses>

what I want to get should be a table showing in HTML 我要获取的应该是以HTML显示的表格

在此处输入图片说明

How about: 怎么样:

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="warehouse-by-country" match="warehouse" use="address/w_country" />

<xsl:template match="/warehouses">
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>Country</th>
                    <th>Name</th>
                    <th>Quantity</th>
                </tr>
                <xsl:for-each select="warehouse[generate-id()=generate-id(key('warehouse-by-country', address/w_country))]">
                    <tr>
                        <td>
                            <xsl:value-of select="address/w_country"/>
                        </td>
                        <xsl:for-each select="key('warehouse-by-country', address/w_country)">
                            <xsl:sort select="items/item/s_qty" data-type="number" order="descending" />
                            <xsl:if test="position()=1">
                                <td>
                                    <xsl:value-of select="w_name"/>
                                </td>
                                <td>
                                    <xsl:value-of select="items/item/s_qty"/>
                                </td>
                            </xsl:if>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM