XSLT 半小时分组

发布于 2024-10-17 21:22:26 字数 1153 浏览 2 评论 0原文

给定这个 XML:

<root>
  <row>
    <time>08:00</time>
    <sales>800</sales>
  </row>
  <row>
    <time>08:15</time>
    <sales>815</sales>
  </row>
  <row>
    <time>08:30</time>
    <sales>830</sales>
  </row>
  <row>
    <time>08:45</time>
    <sales>845</sales>
  </row>
  <row>
    <time>11:00</time>
    <sales>1100</sales>
  </row>
  <row>
    <time>11:45</time>
    <sales>1145</sales>
  </row>
  <row>
    <time>14:15</time>
    <sales>1415</sales>
  </row>
  <row>
    <time>14:30</time>
    <sales>1430</sales>
  </row>
</root>

我试图找到一种通过按 30 分钟间隔汇总销售额来进行 XSLT 转换的方法。我可以使用 MUENCHIAN 方法按每小时 60 分钟的间隔进行汇总,但我不能使用它 30 分钟,因为我需要一个自定义函数来执行此操作(但我不能使用 XSLT 2.0,也不能使用 .Net 的自定义函数)。请帮忙!

预期输出是这样的:

30 minute
08:00 $1600  
08:30 $1675 
11:00 $1100 
11:30 $1145 
14:00 $1415 
14:30 $1430 

Given this XML:

<root>
  <row>
    <time>08:00</time>
    <sales>800</sales>
  </row>
  <row>
    <time>08:15</time>
    <sales>815</sales>
  </row>
  <row>
    <time>08:30</time>
    <sales>830</sales>
  </row>
  <row>
    <time>08:45</time>
    <sales>845</sales>
  </row>
  <row>
    <time>11:00</time>
    <sales>1100</sales>
  </row>
  <row>
    <time>11:45</time>
    <sales>1145</sales>
  </row>
  <row>
    <time>14:15</time>
    <sales>1415</sales>
  </row>
  <row>
    <time>14:30</time>
    <sales>1430</sales>
  </row>
</root>

I am trying to find a way to XSLT transform by summarizing sales by 30 minute intervals. I can summarize by hourly intervals by 60 minutes using MUENCHIAN method, but I cannot use it for 30 minute since I need a custom function to do so (but I cannot use XSLT 2.0, nor .Net's custom functions). Please help!

The expected output is this:

30 minute
08:00 $1600  
08:30 $1675 
11:00 $1100 
11:30 $1145 
14:00 $1415 
14:30 $1430 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔书几行 2024-10-24 21:22:26

解决方案 1。

此转换

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

 <xsl:key name="krowsInHalfHour" match=
  "row[not((substring-after(time,':')+30) mod 30 = 0)]"
  use="generate-id(
        preceding-sibling::row
          [60*substring-before(time,':')
          +
           substring-after(time,':')
          >=
           60*substring-before(current()/time,':')
          +
           substring-after(current()/time,':')

          -
           substring-after(current()/time,':') mod 30

          ]
             [1]
                   )
  "/>

 <xsl:template match=
  "row[(substring-after(time,':')+30) mod 30 = 0
     or
      not(
       60*substring-before(preceding-sibling::row[1]/time,':')
          +
           substring-after(preceding-sibling::row[1]/time,':')
       >=
          60*substring-before(time,':')
          +
           substring-after(time,':')

          -
           substring-after(time,':') mod 30
          )
      ]
  ">
  <xsl:variable name="vPrevStartMins" select=
  "60*substring-before(time,':')
  +
   substring-after(time,':')

  -
   substring-after(time,':') mod 30
  "/>
  <xsl:value-of select=
   "concat('
',floor($vPrevStartMins div 60),
           ':', concat(substring('0',($vPrevStartMins mod 60 >0)+1),
                       $vPrevStartMins mod 60
                       )
           )
   "/>
  <xsl:text> 
lt;/xsl:text>
  <xsl:value-of select=
  "sum(sales
      |
       key('krowsInHalfHour',generate-id())/sales)"/>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<root>
    <row>
        <time>08:00</time>
        <sales>800</sales>
    </row>
    <row>
        <time>08:15</time>
        <sales>815</sales>
    </row>
    <row>
        <time>08:30</time>
        <sales>830</sales>
    </row>
    <row>
        <time>08:45</time>
        <sales>845</sales>
    </row>
    <row>
        <time>11:00</time>
        <sales>1100</sales>
    </row>
    <row>
        <time>11:45</time>
        <sales>1145</sales>
    </row>
    <row>
        <time>14:15</time>
        <sales>1415</sales>
    </row>
    <row>
        <time>14:30</time>
        <sales>1430</sales>
    </row>
</root>

产生所需的正确结果:

8:00 $1615
8:30 $1675
11:00 $1100
11:30 $1145
14:00 $1415
14:30 $1430

解决方案 2

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" >
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <my:halfHours>
  <t>00:00</t><t>00:30</t><t>01:00</t><t>01:30</t>
  <t>02:00</t><t>02:30</t><t>03:00</t><t>03:30</t>
  <t>04:00</t><t>04:30</t><t>05:00</t><t>05:30</t>
  <t>06:00</t><t>06:30</t><t>07:00</t><t>07:30</t>
  <t>08:00</t><t>08:30</t><t>09:00</t><t>09:30</t>
  <t>10:00</t><t>10:30</t><t>11:00</t><t>11:30</t>
  <t>12:00</t><t>12:30</t><t>13:00</t><t>13:30</t>
  <t>14:00</t><t>14:30</t><t>15:00</t><t>15:30</t>
  <t>16:00</t><t>16:30</t><t>17:00</t><t>17:30</t>
  <t>18:00</t><t>18:30</t><t>19:00</t><t>19:30</t>
  <t>20:00</t><t>20:30</t><t>21:00</t><t>21:30</t>
  <t>22:00</t><t>22:30</t><t>23:00</t><t>23:30</t>
  <t>24:00</t>
 </my:halfHours>

 <xsl:variable name="vhalfHrs" select=
  "document('')/*/my:halfHours/*"/>

 <xsl:template match="row">
  <xsl:variable name="vStart" select=
  "$vhalfHrs[translate(.,':','')
            >
             translate(current()/time,':','')
             ][1]
                 /preceding-sibling::*[1]
  "/>

  <xsl:variable name="vprecRow" select=
   "preceding-sibling::*[1]"/>

  <xsl:if test=
   "not(translate($vprecRow/time,':','')
       >=
       translate($vStart,':','')
       )">
   <xsl:value-of select="concat('
',$vStart, ' 

当此转换应用于同一个 XML 文档时,再次生成所需的正确结果

08:00 $1615
08:30 $1675
11:00 $1100
11:30 $1145
14:00 $1415
14:30 $1430

说明

  1. 在变量 $vhalfHrs< /code> 我们有一些元素,其值为一天中每半小时的开始时间。

  2. 在匹配每个的模板中,$vStart变量设置为这个半小时的开始时间,其中<​​code>时间row) 的 > 落入。

  3. 变量 $vprecRow 设置为当前 的前一个同级 (row)。

  4. 如果前的时间不晚于开始半小时时间(在$vStart中),则当前行`是第一行在此半小时内有一个。

  5. 我们输出开始的半小时周期时间。

  6. 我们计算并输出 time 位于同一半小时时间段内的所有 row 元素的总和。它们跟随当前的同级,并且它们的时间不大于或等于下一个半小时周期的开始时间。

解决方案 3 (XSLT 2.0):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text"/>

 <xsl:template match="/*">
  <xsl:for-each-group select="row" group-adjacent=
  "(xs:integer(substring-before(time,':'))*60
   +
    xs:integer(substring-after(time,':'))
    )
    idiv 30
  ">
   <xsl:variable name="vStartMinutes"
        select="current-grouping-key()*30"/>
   <xsl:value-of separator="" select=
   "'
',
     format-number($vStartMinutes idiv 60, '00'), ':',
     format-number($vStartMinutes mod 60,'00'),
     ' 

当将此转换应用于与上述相同的 XML 文档时,会产生相同的所需正确结果

08:00 $1615
08:30 $1675
11:00 $1100
11:30 $1145
14:00 $1415
14:30 $1430

解释:

  1. 我们使用 并将 group-adjacent 属性 设置为计算表达式行/时间 所在的 1/2 小时周期的位置。

  2. 大量使用标准函数 current-group()current-grouping-key()

)"/> <xsl:value-of select= "sum(sales|following-sibling::* [not(translate(time,':','') >= translate($vStart/following-sibling::*,':','') ) ] /sales ) "/> </xsl:if> </xsl:template> </xsl:stylesheet>

当此转换应用于同一个 XML 文档时,再次生成所需的正确结果


说明

  1. 在变量 $vhalfHrs< /code> 我们有一些元素,其值为一天中每半小时的开始时间。

  2. 在匹配每个的模板中,$vStart变量设置为这个半小时的开始时间,其中<​​code>时间row) 的 > 落入。

  3. 变量 $vprecRow 设置为当前 的前一个同级 (row)。

  4. 如果前的时间不晚于开始半小时时间(在$vStart中),则当前行`是第一行在此半小时内有一个。

  5. 我们输出开始的半小时周期时间。

  6. 我们计算并输出 time 位于同一半小时时间段内的所有 row 元素的总和。它们跟随当前的同级,并且它们的时间不大于或等于下一个半小时周期的开始时间。

解决方案 3 (XSLT 2.0):


当将此转换应用于与上述相同的 XML 文档时,会产生相同的所需正确结果


解释:

  1. 我们使用 并将 group-adjacent 属性 设置为计算表达式行/时间 所在的 1/2 小时周期的位置。

  2. 大量使用标准函数 current-group()current-grouping-key()

, sum(current-group()/sales/xs:integer(.)) "/> </xsl:for-each-group> </xsl:template> </xsl:stylesheet>

当将此转换应用于与上述相同的 XML 文档时,会产生相同的所需正确结果

解释:

  1. 我们使用 并将 group-adjacent 属性 设置为计算表达式行/时间 所在的 1/2 小时周期的位置。

  2. 大量使用标准函数 current-group()current-grouping-key()

)"/> <xsl:value-of select= "sum(sales|following-sibling::* [not(translate(time,':','') >= translate($vStart/following-sibling::*,':','') ) ] /sales ) "/> </xsl:if> </xsl:template> </xsl:stylesheet>

当此转换应用于同一个 XML 文档时,再次生成所需的正确结果

说明

  1. 在变量 $vhalfHrs< /code> 我们有一些元素,其值为一天中每半小时的开始时间。

  2. 在匹配每个的模板中,$vStart变量设置为这个半小时的开始时间,其中<​​code>时间row) 的 > 落入。

  3. 变量 $vprecRow 设置为当前 的前一个同级 (row)。

  4. 如果前的时间不晚于开始半小时时间(在$vStart中),则当前行`是第一行在此半小时内有一个。

  5. 我们输出开始的半小时周期时间。

  6. 我们计算并输出 time 位于同一半小时时间段内的所有 row 元素的总和。它们跟随当前的同级,并且它们的时间不大于或等于下一个半小时周期的开始时间。

解决方案 3 (XSLT 2.0):

当将此转换应用于与上述相同的 XML 文档时,会产生相同的所需正确结果

解释:

  1. 我们使用 并将 group-adjacent 属性 设置为计算表达式行/时间 所在的 1/2 小时周期的位置。

  2. 大量使用标准函数 current-group()current-grouping-key()

Solution 1.

This transformation:

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

 <xsl:key name="krowsInHalfHour" match=
  "row[not((substring-after(time,':')+30) mod 30 = 0)]"
  use="generate-id(
        preceding-sibling::row
          [60*substring-before(time,':')
          +
           substring-after(time,':')
          >=
           60*substring-before(current()/time,':')
          +
           substring-after(current()/time,':')

          -
           substring-after(current()/time,':') mod 30

          ]
             [1]
                   )
  "/>

 <xsl:template match=
  "row[(substring-after(time,':')+30) mod 30 = 0
     or
      not(
       60*substring-before(preceding-sibling::row[1]/time,':')
          +
           substring-after(preceding-sibling::row[1]/time,':')
       >=
          60*substring-before(time,':')
          +
           substring-after(time,':')

          -
           substring-after(time,':') mod 30
          )
      ]
  ">
  <xsl:variable name="vPrevStartMins" select=
  "60*substring-before(time,':')
  +
   substring-after(time,':')

  -
   substring-after(time,':') mod 30
  "/>
  <xsl:value-of select=
   "concat('
',floor($vPrevStartMins div 60),
           ':', concat(substring('0',($vPrevStartMins mod 60 >0)+1),
                       $vPrevStartMins mod 60
                       )
           )
   "/>
  <xsl:text> 
lt;/xsl:text>
  <xsl:value-of select=
  "sum(sales
      |
       key('krowsInHalfHour',generate-id())/sales)"/>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the provided XML document:

<root>
    <row>
        <time>08:00</time>
        <sales>800</sales>
    </row>
    <row>
        <time>08:15</time>
        <sales>815</sales>
    </row>
    <row>
        <time>08:30</time>
        <sales>830</sales>
    </row>
    <row>
        <time>08:45</time>
        <sales>845</sales>
    </row>
    <row>
        <time>11:00</time>
        <sales>1100</sales>
    </row>
    <row>
        <time>11:45</time>
        <sales>1145</sales>
    </row>
    <row>
        <time>14:15</time>
        <sales>1415</sales>
    </row>
    <row>
        <time>14:30</time>
        <sales>1430</sales>
    </row>
</root>

produces the wanted, correct result:

8:00 $1615
8:30 $1675
11:00 $1100
11:30 $1145
14:00 $1415
14:30 $1430

Solution 2:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" >
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <my:halfHours>
  <t>00:00</t><t>00:30</t><t>01:00</t><t>01:30</t>
  <t>02:00</t><t>02:30</t><t>03:00</t><t>03:30</t>
  <t>04:00</t><t>04:30</t><t>05:00</t><t>05:30</t>
  <t>06:00</t><t>06:30</t><t>07:00</t><t>07:30</t>
  <t>08:00</t><t>08:30</t><t>09:00</t><t>09:30</t>
  <t>10:00</t><t>10:30</t><t>11:00</t><t>11:30</t>
  <t>12:00</t><t>12:30</t><t>13:00</t><t>13:30</t>
  <t>14:00</t><t>14:30</t><t>15:00</t><t>15:30</t>
  <t>16:00</t><t>16:30</t><t>17:00</t><t>17:30</t>
  <t>18:00</t><t>18:30</t><t>19:00</t><t>19:30</t>
  <t>20:00</t><t>20:30</t><t>21:00</t><t>21:30</t>
  <t>22:00</t><t>22:30</t><t>23:00</t><t>23:30</t>
  <t>24:00</t>
 </my:halfHours>

 <xsl:variable name="vhalfHrs" select=
  "document('')/*/my:halfHours/*"/>

 <xsl:template match="row">
  <xsl:variable name="vStart" select=
  "$vhalfHrs[translate(.,':','')
            >
             translate(current()/time,':','')
             ][1]
                 /preceding-sibling::*[1]
  "/>

  <xsl:variable name="vprecRow" select=
   "preceding-sibling::*[1]"/>

  <xsl:if test=
   "not(translate($vprecRow/time,':','')
       >=
       translate($vStart,':','')
       )">
   <xsl:value-of select="concat('
',$vStart, ' 

when this transformation is applied on the same XML document, again the wanted, correct result is produced:

08:00 $1615
08:30 $1675
11:00 $1100
11:30 $1145
14:00 $1415
14:30 $1430

Explanation:

  1. In the variable $vhalfHrs we have elements whose values are the starting time of every half-hour period during the day.

  2. In the template that matches every row, the $vStart variable is set ti this half-hour start-time, in which the time of the current node (row) falls into.

  3. The variable $vprecRow is set to the preceding sibling (row) of the current row.

  4. If the time of the preceding row is not later than the start-half-hour-time (in $vStart), then the currentrow` is the first one in this half-hour period.

  5. We output the starting half-hour period time.

  6. We calculate and output the sum of all row elements whose time is in this same half-hour time period. They are following siblings of the current row and their time is not greater or equal the start of the next half-hour period.

Solution 3 (XSLT 2.0):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text"/>

 <xsl:template match="/*">
  <xsl:for-each-group select="row" group-adjacent=
  "(xs:integer(substring-before(time,':'))*60
   +
    xs:integer(substring-after(time,':'))
    )
    idiv 30
  ">
   <xsl:variable name="vStartMinutes"
        select="current-grouping-key()*30"/>
   <xsl:value-of separator="" select=
   "'
',
     format-number($vStartMinutes idiv 60, '00'), ':',
     format-number($vStartMinutes mod 60,'00'),
     ' 

when this transformation is applied on the same XML document as above, the same wanted, correct result is produced:

08:00 $1615
08:30 $1675
11:00 $1100
11:30 $1145
14:00 $1415
14:30 $1430

Explanation:

  1. We are using <xsl:for-each-group> with the group-adjacent attribute set as an expression calculating the position of the 1/2 hour period in which a row/time is.

  2. Heavy use of the standard functions current-group() and current-grouping-key() .

)"/> <xsl:value-of select= "sum(sales|following-sibling::* [not(translate(time,':','') >= translate($vStart/following-sibling::*,':','') ) ] /sales ) "/> </xsl:if> </xsl:template> </xsl:stylesheet>

when this transformation is applied on the same XML document, again the wanted, correct result is produced:


Explanation:

  1. In the variable $vhalfHrs we have elements whose values are the starting time of every half-hour period during the day.

  2. In the template that matches every row, the $vStart variable is set ti this half-hour start-time, in which the time of the current node (row) falls into.

  3. The variable $vprecRow is set to the preceding sibling (row) of the current row.

  4. If the time of the preceding row is not later than the start-half-hour-time (in $vStart), then the currentrow` is the first one in this half-hour period.

  5. We output the starting half-hour period time.

  6. We calculate and output the sum of all row elements whose time is in this same half-hour time period. They are following siblings of the current row and their time is not greater or equal the start of the next half-hour period.

Solution 3 (XSLT 2.0):


when this transformation is applied on the same XML document as above, the same wanted, correct result is produced:


Explanation:

  1. We are using <xsl:for-each-group> with the group-adjacent attribute set as an expression calculating the position of the 1/2 hour period in which a row/time is.

  2. Heavy use of the standard functions current-group() and current-grouping-key() .

, sum(current-group()/sales/xs:integer(.)) "/> </xsl:for-each-group> </xsl:template> </xsl:stylesheet>

when this transformation is applied on the same XML document as above, the same wanted, correct result is produced:

Explanation:

  1. We are using <xsl:for-each-group> with the group-adjacent attribute set as an expression calculating the position of the 1/2 hour period in which a row/time is.

  2. Heavy use of the standard functions current-group() and current-grouping-key() .

)"/> <xsl:value-of select= "sum(sales|following-sibling::* [not(translate(time,':','') >= translate($vStart/following-sibling::*,':','') ) ] /sales ) "/> </xsl:if> </xsl:template> </xsl:stylesheet>

when this transformation is applied on the same XML document, again the wanted, correct result is produced:

Explanation:

  1. In the variable $vhalfHrs we have elements whose values are the starting time of every half-hour period during the day.

  2. In the template that matches every row, the $vStart variable is set ti this half-hour start-time, in which the time of the current node (row) falls into.

  3. The variable $vprecRow is set to the preceding sibling (row) of the current row.

  4. If the time of the preceding row is not later than the start-half-hour-time (in $vStart), then the currentrow` is the first one in this half-hour period.

  5. We output the starting half-hour period time.

  6. We calculate and output the sum of all row elements whose time is in this same half-hour time period. They are following siblings of the current row and their time is not greater or equal the start of the next half-hour period.

Solution 3 (XSLT 2.0):

when this transformation is applied on the same XML document as above, the same wanted, correct result is produced:

Explanation:

  1. We are using <xsl:for-each-group> with the group-adjacent attribute set as an expression calculating the position of the 1/2 hour period in which a row/time is.

  2. Heavy use of the standard functions current-group() and current-grouping-key() .

温柔嚣张 2024-10-24 21:22:26

如果没有前面的轴,此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:key name="kRowByHalfHour"
             match="row"
             use="floor((substring(time,1,2)*60+substring(time,4))div 30)"/>
    <xsl:template match="row[count(.|key('kRowByHalfHour',
                                         floor((substring(time,1,2) * 60
                                                 + substring(time,4)) div 30)
                                     )[1]
                             )=1
                         ]">
        <xsl:variable name="vKey"
             select="floor((substring(time,1,2)*60+substring(time,4))div 30)"/>
        <xsl:value-of
             select="concat(format-number(floor($vKey * 30 div 60),'00'),':',
                            format-number(($vKey * 30) mod 60,'00'),' 

输出:

08:00 $1615
08:30 $1675
11:00 $1100
11:30 $1145
14:00 $1415
14:30 $1430

编辑 2:更好的键值,标准化时间允许参数化(通过 document() 函数或两阶段转换):如果我们采取关键计算作为复杂性度量,这将是 O(N)

, sum(key('kRowByHalfHour',$vKey)/sales),' ' )"/> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>

输出:

编辑 2:更好的键值,标准化时间允许参数化(通过 document() 函数或两阶段转换):如果我们采取关键计算作为复杂性度量,这将是 O(N)

Without preceding axis, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:key name="kRowByHalfHour"
             match="row"
             use="floor((substring(time,1,2)*60+substring(time,4))div 30)"/>
    <xsl:template match="row[count(.|key('kRowByHalfHour',
                                         floor((substring(time,1,2) * 60
                                                 + substring(time,4)) div 30)
                                     )[1]
                             )=1
                         ]">
        <xsl:variable name="vKey"
             select="floor((substring(time,1,2)*60+substring(time,4))div 30)"/>
        <xsl:value-of
             select="concat(format-number(floor($vKey * 30 div 60),'00'),':',
                            format-number(($vKey * 30) mod 60,'00'),' 

Output:

08:00 $1615
08:30 $1675
11:00 $1100
11:30 $1145
14:00 $1415
14:30 $1430

Edit 2: Even better key value with normalized time allowing parameterization (by document() function or two phase transformation): if we take key calculation as complexity measure, this will be O(N)

, sum(key('kRowByHalfHour',$vKey)/sales),' ' )"/> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>

Output:

Edit 2: Even better key value with normalized time allowing parameterization (by document() function or two phase transformation): if we take key calculation as complexity measure, this will be O(N)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文