小编Rya*_*anB的帖子

XSLT 3.0流与分组和Sum/Accumulator

我想弄清楚如何在需要分组(具有任意数量的组)和对组进行求和的场景中使用XSLT Streaming(以减少内存使用).到目前为止,我还没有找到任何例子.这是一个示例XML

<?xml version='1.0' encoding='UTF-8'?>
  <Data>
    <Entry>
      <Genre>Fantasy</Genre>
      <Condition>New</Condition>
      <Format>Hardback</Format>
      <Title>Birds</Title>
      <Count>3</Count>
    </Entry>
    <Entry>
      <Genre>Fantasy</Genre>
      <Condition>New</Condition>
      <Format>Hardback</Format>
      <Title>Cats</Title>
      <Count>2</Count>
    </Entry>
    <Entry>
      <Genre>Non-Fiction</Genre>
      <Condition>New</Condition>
      <Format>Paperback</Format>
      <Title>Dogs</Title>
      <Count>4</Count>
    </Entry>
 </Data>
Run Code Online (Sandbox Code Playgroud)

在XSLT 2.0中,我将使用它按类型,条件和格式分组并对计数求和.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes" />
  <xsl:template match="/">
     <xsl:call-template name="body"/>
  </xsl:template>
  <xsl:template name="body">
    <xsl:for-each-group select="Data/Entry" group-by="concat(Genre,Condition,Format)">
      <xsl:value-of select="Genre"/>
      <xsl:value-of select="Condition"/>
      <xsl:value-of select="Format"/>
      <xsl:value-of select="sum(current-group()/Count)"/>
    </xsl:for-each-group>
  </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

对于输出,我会得到两行,Fantasy,New,Hardback的总和为5,非小说,New,平装本的总和为4.

显然,这不适用于Streaming,因为sum访问整个组.我想我需要两次遍历文档.我第一次可以构建组的映射(如果还没有存在,则创建一个新组).第二次问题是我还需要一个具有匹配组的规则的每个组的累加器,并且似乎你不能创建动态累加器.

有没有办法动态创建累加器?有没有其他/更简单的方法来实现流媒体?

xslt xslt-grouping xslt-3.0

3
推荐指数
1
解决办法
945
查看次数

标签 统计

xslt ×1

xslt-3.0 ×1

xslt-grouping ×1