XSLT - 按属性分组和排序

时间:2011-10-10 22:29:57

标签: xslt

我有一个包含不同日期价格的xml

<tour id="12314">
  <available date="2012-04-19" price="533" /> 
  <available date="2012-05-09" price="670" /> 
  <available date="2012-05-25" price="600" /> 
  <available date="2012-06-05" price="710" /> 
  <available date="2012-06-08" price="710" /> 
  <available date="2012-06-15" price="710" /> 
  <available date="2012-06-20" price="705" /> 
</tour>

我的要求是使用XSLT获取每月最便宜的节点  例如:所需的输出是:

 <available dt="2012-04-19" price="533" /> 
 <available dt="2012-05-25" price="600" /> 
 <available dt="2012-06-20" price="705" /> 

我开始按如下方式对可用节点进行排序,但我不确定如何按月以最便宜的价格对节点进行分组

<xsl:for-each select="tour[@id='12314']/available">
    <xsl:sort select="substring(@dt,1,7)"/>
    <xsl:sort select="@price"/>
    <!-- I would like to access the available node which has the cheapest price for each month -->
</xsl:for-each>

非常感谢任何帮助

2 个答案:

答案 0 :(得分:2)

使用Muenchian方法和简单排序的XSLT 1.0解决方案:

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="months" match="available" use="substring(@date, 6, 2)"/>

  <xsl:template match="/tour[@id = '12314']">
    <result>
      <xsl:for-each select="./available[generate-id() = generate-id(key('months',substring(@date, 6, 2) )[1])]">
        <xsl:for-each select="key('months',substring(@date, 6, 2))">
          <xsl:sort select="@price" order="ascending"/>
          <xsl:if test="position() = 1">
            <xsl:copy-of select="current()"/>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>
    </result>
  </xsl:template>
</xsl:stylesheet>

将上述转换应用于此xml:

 <tour id="12314">
  <available date="2012-04-19" price="533" /> 
  <available date="2012-05-09" price="670" /> 
  <available date="2012-05-25" price="600" /> 
  <available date="2012-06-05" price="710" /> 
  <available date="2012-06-08" price="710" /> 
  <available date="2012-06-15" price="710" /> 
  <available date="2012-06-20" price="705" /> 
</tour>

输出结果为:

<?xml version="1.0" encoding="UTF-8"?>
<result>
   <available date="2012-04-19" price="533"/>
   <available date="2012-05-25" price="600"/>
   <available date="2012-06-20" price="705"/>
</result>

逻辑:首先我根据@date属性的子字符串对可用节点进行分组,然后对于每个独特的月份我收集所有可用节点按升序对它们进行排序,然后按照定义打印最小价格的节点第一个节点由于排序。希望它有所帮助:)

答案 1 :(得分:1)

我提供了三个替代解决方案每个简短(没有嵌套<xsl:for-each>且没有排序)。如果可能的话,我建议使用XSLT 2.0解决方案。

予。两种替代XSLT 1.0解决方案:

<强> 1。没有钥匙:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="available">
  <xsl:if test=
  "not(@price
      >
       ( preceding-sibling::available
       |
        following-sibling::available
        )
           [substring(@date, 1, 7)
           =
            substring(current()/@date, 1, 7)
           ]
           /@price
       )">
   <xsl:call-template name="identity"/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

<强> 2。使用密钥:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kDateByMonth" match="available"
          use="substring(@date, 1, 7)"/>

 <xsl:template match=
  "available
      [generate-id()
      =
       generate-id(key('kDateByMonth',
                       substring(@date, 1, 7)
                       )[1]
                   )
      ]
  ">
  <xsl:variable name="vsameMonth" select=
   "key('kDateByMonth',
         substring(@date, 1, 7)
         )
   "/>

  <xsl:copy-of select=
    "$vsameMonth[not(@price > $vsameMonth/@price)][1]
    "/>
 </xsl:template>
</xsl:stylesheet>

当上述两个转换中的任何一个应用于提供的XML文档时

                    

产生了想要的正确结果

<tour id="12314">
   <available date="2012-04-19" price="533"/>
   <available date="2012-05-25" price="600"/>
   <available date="2012-06-20" price="705"/>
</tour>

注意:在问题中,如果一个月内多个巡视的最低价格相同,则未指定输出内容。第一个转换将输出所有这样的游览(并且可能会给读者选择),而第二个转换每月仅输出一个这样的游览。可以修改这两个转换以实现其他行为。

<强> II。 XSLT 2.0解决方案:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <xsl:for-each-group select="*"
                       group-by="substring(@date, 1,7)">
    <xsl:copy-of select=
     "current-group()
        [@price
        =
         min(current-group()/@price/number())
         ]
         [1]"/>
  </xsl:for-each-group>
 </xsl:template>
</xsl:stylesheet>