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

时间:2019-03-26 07:56:25

标签: xml xslt-1.0

我正在使用XSLT版本1转换XML,规则是分别在两个国家中找到最大数量,并且xml结构有点复杂,我应该使用什么功能?

我已经使用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>

我想要得到的应该是显示为HTML的表格

enter image description here

1 个答案:

答案 0 :(得分:1)

怎么样:

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>