xslt 2.0多重分组

时间:2013-10-30 10:53:05

标签: xslt grouping xslt-2.0 xslt-grouping

我的问题:如何应用双重(或多重)分组?

以下是源XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <row>
        <Type>1</Type>
        <WeaNr>100519</WeaNr>
    </row>
    <row>
        <Type>2</Type>
        <WeaNr>100519</WeaNr>
        <ETADC_SKU>2007925</ETADC_SKU>
        <CrossDock>N</CrossDock>
    </row>
    <row>
        <Type>2</Type>
        <WeaNr>100519</WeaNr>
        <ETADC_SKU>12007925</ETADC_SKU>
        <CrossDock>N</CrossDock>
    </row>
    <row>
        <Type>2</Type>
        <WeaNr>100519</WeaNr>
        <ETADC_SKU>200792ww5</ETADC_SKU>
        <CrossDock>Y</CrossDock>
    </row>
    <row>
        <Type>1</Type>
        <WeaNr>100520</WeaNr>
    </row>
    <row>
        <Type>2</Type>
        <WeaNr>100520</WeaNr>
        <ETADC_SKU>2007925444</ETADC_SKU>
        <CrossDock>N</CrossDock>
    </row>
    <row>
        <Type>2</Type>
        <WeaNr>100520</WeaNr>
        <ETADC_SKU>2007925333</ETADC_SKU>
        <CrossDock>Y</CrossDock>
    </row>
    <row>
        <Type>2</Type>
        <WeaNr>100520</WeaNr>
        <ETADC_SKU>204445333</ETADC_SKU>
        <CrossDock>Y</CrossDock>
    </row>
</root>

我希望按WeaNrCrossDock

进行分组

本案例中的预期结果为4组:

1. WeaNr=100519 and CrossDock=N
2. WeaNr=100519 and CrossDock=Y
3. WeaNr=100520 and CrossDock=N
4. WeaNr=100520 and CrossDock=Y

只需一个字段进行分组,就像WeaNr一样简单:

<xsl:for-each-group select="row" group-by="WeaNr">

那么如何应用双重(或多重)分组?

1 个答案:

答案 0 :(得分:9)

你会group-by某些字符串是两者的组合,例如

<xsl:for-each-group select="row" group-by="concat(WeaNr, '|', CrossDock)">

或者使用两个嵌套级别的for-each-group

<xsl:for-each-group select="row" group-by="WeaNr">
  <xsl:for-each-group select="current-group()" group-by="CrossDock">

如果您在position()的正文中使用for-each-group函数,这两种方法之间的区别很明显 - 在concat情况下,您将获得1到4的位置值,在嵌套的情况下,你将获得1,2,1,2(因为position()最近的封闭for-each-group确定。同样,last()案例中的concat为4,嵌套案例中为2。