XSL Group by Item Model

时间:2013-11-16 10:18:18

标签: xml xslt

我的问题是我想按模型为每个项目分组,但我只想加载单个实体的详细信息

<Search-Request search-term="1 tb">
    <Items>
        <Item href="SEA1TBST31000524AS.jpg" model="ST31000524AS">
            <Name>Seagate Hard disk 1TB ST31000524AS 3.5"</Name>
            <Price>60.50</Price>
            <SupplierCode>TECSEA1TB</SupplierCode>
            <Supplier>TEC001</Supplier>
            <Manufacturer>Seagate</Manufacturer>
            <CustomerReviews>
                <Review>
                    <Reviewer>Hayley Park</Reviewer>
                    <Rating>5</Rating>
                    <Review>I bought this because my boyfriend suggested it. But I am glad I did since it has a good amount of storage. And was quite cheap.</Review>
                </Review>
                <Review>
                    <Reviewer>Bill Gates</Reviewer>
                    <Rating>3</Rating>
                    <Review>Not that big storage wise but can't complain too much about price.</Review>
                </Review>
                <Review>
                    <Reviewer>Jean Pierre Said</Reviewer>
                    <Rating>4</Rating>
                    <Review>So far I've been using this product for a year and have no disregrets since it is much better than the previous hdd which I had which was only 256GB.</Review>
                </Review>
            </CustomerReviews>
        </Item>
        <Item href="" model="ST31000524AS">
            <Name>Seagate Hard disk 1TB ST31000524AS 3.5 inch</Name>
            <Price>55.50</Price>
            <SupplierCode>SCASEA1TB</SupplierCode>
            <Supplier>SCA001</Supplier>
            <Manufacturer>Seagate</Manufacturer>
            <CustomerReviews>
                <Review>
                    <Reviewer>Kyle Werner</Reviewer>
                    <Rating>4</Rating>
                    <Review>Very reliable product and at a great price.</Review>
                </Review>
                <Review>
                    <Reviewer>Scar Russo</Reviewer>
                    <Rating>5</Rating>
                    <Review>My only regret is that I didn't buy this after the price drop. Overall the product is great value for money.</Review>
                </Review>
                <Review>
                    <Reviewer>Stan Lee</Reviewer>
                    <Rating>1</Rating>
                    <Review>I don't know if it's me but this product burned out on me after just 2 weeks.</Review>
                </Review>
            </CustomerReviews>
        </Item>
     </Items>

因此上面的XML将包含遇到的第一个元素

<Item href="SEA1TBST31000524AS.jpg" model="ST31000524AS">
         <Name>Seagate Hard disk 1TB ST31000524AS 3.5"</Name>
         <Manufacturer>Seagate</Manufacturer>  
</Item>

我想知道我是否可以选择两种价格,然后只选择要显示的最佳价格。其他价格可以列在工具提示中。例如。

并且明智地将它们结合起来

我知道这不是一件简单的事。

我很容易就每个项目都列出了每个项目。

但我希望它看起来更精致,但要求是没有使用高级语言代码所以我问你们这是否可能。

如果可能的话,可以将项目分成多个页面,例如每页显示5个。虽然这不是必要的,但我只是想知道是否有可能。

1 个答案:

答案 0 :(得分:1)

我确实找到了如何做我需要的事情

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="Price">
<xsl:if test="position() = 1">
<li><xsl:value-of select="."/></li>
</xsl:if>
</xsl:template>

<xsl:template match="Search-Request">
<html>
<body>
      <xsl:for-each-group select="Items/Item" group-by="@model">
        <xsl:sort select="@model" data-type="text" order="descending"/>
        <p><xsl:value-of select="@model"/></p>
        <ol>
<xsl:apply-templates select="current-group()/Price">
<xsl:sort select="." data-type="text" order="ascending"/>
</xsl:apply-templates>
        </ol>
      </xsl:for-each-group>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

现在我只需要应用剩下的元素就可以完成了。