我的问题:如何应用双重(或多重)分组?
以下是源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>
我希望按WeaNr
和CrossDock
本案例中的预期结果为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">
那么如何应用双重(或多重)分组?
答案 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。