XSL for-each-group 使用两个 group-by 参数:首先按日期分组,然后按名称分组 [英] XSL for-each-group using two group-by parameters: first group by date then by name

查看:28
本文介绍了XSL for-each-group 使用两个 group-by 参数:首先按日期分组,然后按名称分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试在 pdf 文件中创建分组我必须在另一个分组中使用分组.例如按日期分组,然后按名称分组,然后按城市...

Hello I'm trying to create grouping in the pdf file and I have to use grouping inside another grouping. f.e. group by date, then group by name, then by city...

xml:
<record>
<name>Palace1</name>
  <info>
     <date>2012-01-01</date> 
     <city>Random1</city> 
  </info>
  <info>
     <date>2012-01-01</date> 
     <city>SuperRandom</city> 
  </info>
  <info>
     <date>2012-01-02</date>
     <city>Random22</city>       
  </info>
   ...
</record>
<record>
<name>Palace2</name>
  <info>
    <date>2012-01-01</date>  
     <city>Random99</city>     
  </info>
  <info>
    <date>2012-01-02</date> 
     <city>Random1</city>     
  </info>
   ...
</record>
...

假设我们需要按 2012-01-01 到 2012-01-01 的日期对记录进行分组,然后按名称分组

So lets say we need to group our records by date from 2012-01-01 to 2012-01-01 and the group them by name

日期 2012-01-01

Place1

随机 1

超级随机

Palace2

Random99

日期 2012-01-02

Palace1

Random22

Palace2

随机 1

所以我正在使用

<xsl:for-each-group select="dt:record" group-by="dt:info/dt:date">
<xsl:sort select="dt:date" order="ascending"/>

<fo:block font-weight="bold"> Date: <xsl:value-of select="format-dateTime(dt:date,'[Y0001].[M01].[D01]','en',(),'lt')"/></fo:block>

 <xsl:for-each select="current-group()"> //here im guessing we should put another for- each-group



 <xsl:for-each-group select="parent::dt:info/dt:record" group-by="dt:name">
 <fo:block>Place1 <xsl:value-of select="dt:name"><fo:block>
 <xsl:for-each select="current-group()">

 <fo:block> <xsl:value-of select="dt:info/dt:city"></fo:block>

 </xsl:for-each>
 </xsl:for-each-group>


</xsl:for-each>                 

</xsl:for-each-group>

但这不起作用......出于某种原因,我得到了更多的记录名称

but this doesn't work... for some reason I get way more record names then I should

推荐答案

这里是如何进行这样的分组:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
     <xsl:for-each-group select="*/info" group-by="date">
        <xsl:sort select="date"/>
Date: <xsl:value-of select="date"/>

          <xsl:for-each-group select="current-group()" group-by="../name">
            <xsl:value-of select="concat('&#xA;  ',../name)"/>

            <xsl:for-each-group select="current-group()" group-by="city">
              <xsl:value-of select="concat('&#xA;    ', city)"/>
            </xsl:for-each-group>
          </xsl:for-each-group>
     </xsl:for-each-group>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于从非格式良好的提供的 XML 派生的格式良好的 XML 文档时:

<t>
    <record>
        <name>Palace1</name>
        <info>
            <date>2012-01-01</date>
            <city>Random1</city>
        </info>
        <info>
            <date>2012-01-01</date>
            <city>SuperRandom</city>
        </info>
        <info>
            <date>2012-01-02</date>
            <city>Random22</city>
        </info>    ... 
    </record>
    <record>
        <name>Palace2</name>
        <info>
            <date>2012-01-01</date>
            <city>Random99</city>
        </info>
        <info>
            <date>2012-01-02</date>
            <city>Random1</city>
        </info>    ... 
    </record>
</t>

产生了想要的、正确分组的结果:

Date: 2012-01-01
  Palace1
    Random1
    SuperRandom
  Palace2
    Random99
Date: 2012-01-02
  Palace1
    Random22
  Palace2
    Random1

这篇关于XSL for-each-group 使用两个 group-by 参数:首先按日期分组,然后按名称分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆