Reputation: 27
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?
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
<?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
Upvotes: 0
Views: 38
Reputation: 117073
How about:
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>
Upvotes: 1