【问题标题】:XSL Group by Item ModelXSL 按项目模型分组
【发布时间】:2013-11-16 10:18:18
【问题描述】:

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

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

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

并明智地将它们组合起来

我知道这并不简单。

我很容易就能在每个项目上用一个 for each 列出它们。

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

如果可能的话,可以将项目拆分为多个页面,例如每页显示 5 个。虽然这不是必需的,但它只是美观的东西,我想知道它是否可能。

【问题讨论】:

  • 这是一个非常简单的任务。您要查找的关键字是“Muenchian grouping”和“&lt;xsl:key&gt;”。仅在 StackOverflow 上就有数百个示例。您需要指定输出 HTML 的样子(我认为到目前为止您还没有这样做)。那请大家自己尝试解决,遇到卡住的时候发个代码示例。

标签: xml xslt


【解决方案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>

现在我只需要应用其余的元素,它就完成了。

【讨论】:

  • +1 就是这样。对于您的下一个 XSLT 问题,不要忘记提及您使用的是 XSLT 2.0。
  • @Tomalak 好吧,我的目标不是 xslt 2,但我发现解决方案似乎更简单。
  • 是的。无论如何都会比 XSLT 1.0 更简单。而这正是解决这个问题的正确方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多