XSLT 3.0流与分组和Sum / Accumulator

时间:2017-05-31 14:49:55

标签: xslt xslt-grouping xslt-3.0

我试图弄清楚如何在需要分组(具有任意数量的组)和对组进行求和的场景中使用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>

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

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

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

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

1 个答案:

答案 0 :(得分:6)

为了能够在XSLT 3.0中使用流分组,我看到的一个选项是首先使用类似

的样式表将基于元素的数据转换为基于属性的数据
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:mode streamable="yes" on-no-match="shallow-copy"/>

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="Entry/*">
        <xsl:attribute name="{name()}" namespace="{namespace-uri()}" select="."/>
    </xsl:template>

</xsl:stylesheet>

那么你可以完美地使用流式分组(就流式group-by而言是可能的,据我所知,将需要一些缓冲)如下:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:mode streamable="yes"/>

    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:fork>
            <xsl:for-each-group select="Data/Entry" composite="yes" group-by="@Genre, @Condition, @Format">
                <xsl:value-of select="current-grouping-key(), sum(current-group()/@Count)"/>
                <xsl:text>&#10;</xsl:text>
            </xsl:for-each-group>
        </xsl:fork>
    </xsl:template>

</xsl:stylesheet>

我不知道首先创建一个以属性为中心的文档是一个选项,但我认为最好在答案中与代码分享建议,而不是试图将它们放入评论中。而XSLT Streaming Chained Transform中的答案显示了如何使用Saxon 9与Java或Scala链接两个流转换,而无需为第一个转换步骤编写临时输出文件。

至于在原始输入格式上使用copy-of,Saxon 9.7 EE评估以下内容为streamable并以正确的结果执行:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:mode streamable="yes"/>

    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:for-each-group select="copy-of(Data/Entry)" composite="yes"
            group-by="Genre, Condition, Format">
            <xsl:value-of select="current-grouping-key(), sum(current-group()/Count)"/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each-group>
    </xsl:template>

</xsl:stylesheet>

我不确定它比正常的基于树的分组消耗更少的内存。也许您可以使用实际输入数据进行衡量。

作为第三种选择,要使用您似乎想要做的地图,这里有一个xsl:iterate示例,它遍历Entry元素,收集累积的Count值地图:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map" exclude-result-prefixes="xs math map"
    version="3.0">

    <xsl:mode streamable="yes"/>

    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:iterate select="Data/Entry">
            <xsl:param name="groups" as="map(xs:string, xs:integer)" select="map{}"/>
            <xsl:on-completion>
                <xsl:value-of select="map:keys($groups)!(. || ' ' || $groups(.))" separator="&#10;"/>
            </xsl:on-completion>
            <xsl:variable name="current-entry" select="copy-of()"/>
            <xsl:variable name="key"
                select="string-join($current-entry/(Genre, Condition, Format), '|')"/>
            <xsl:next-iteration>
                <xsl:with-param name="groups"
                    select="
                        if (map:contains($groups, $key)) then
                            map:put($groups, $key, map:get($groups, $key) + xs:integer($current-entry/Count))
                        else
                            map:put($groups, $key, xs:integer($current-entry/Count))"
                />
            </xsl:next-iteration>
        </xsl:iterate>
    </xsl:template>

</xsl:stylesheet>