XSL for-each-group使用两个分组参数:第一个按日期分组,然后按名称分组

时间:2012-01-04 12:44:36

标签: xslt xsl-fo

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

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对记录进行分组,并将它们按名称分组

日期2012-01-01

PLACE1

Random1

SuperRandom

Palace2

Random99

日期2012-01-02

Palace1

Random22

Palace2

Random1

所以我正在使用

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

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

2 个答案:

答案 0 :(得分:4)

以下是如何进行此类分组

<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

答案 1 :(得分:0)

这是一个例子,它输出HTML,没有XSL-FO,但分组当然是相同的:

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

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="root">
    <xsl:for-each-group select="record/info" group-by="xs:date(date)">
      <xsl:sort select="current-grouping-key()"/>
      <div>
        <h2><xsl:value-of select="current-grouping-key()"/></h2>
        <xsl:for-each-group select="current-group()" group-by="parent::record/name">
          <div>
            <h3><xsl:value-of select="current-grouping-key()"/></h3>
            <ul>
              <xsl:for-each select="current-group()">
                <li>
                  <xsl:value-of select="city"/>
                </li>
              </xsl:for-each>
            </ul>
          </div>
        </xsl:for-each-group>
      </div>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

申请时

<root>
<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>
</root>

我得到了

<div>
   <h2>2012-01-01</h2>
   <div>
      <h3>Palace1</h3>
      <ul>
         <li>Random1</li>
         <li>SuperRandom</li>
      </ul>
   </div>
   <div>
      <h3>Palace2</h3>
      <ul>
         <li>Random99</li>
      </ul>
   </div>
</div>
<div>
   <h2>2012-01-02</h2>
   <div>
      <h3>Palace1</h3>
      <ul>
         <li>Random22</li>
      </ul>
   </div>
   <div>
      <h3>Palace2</h3>
      <ul>
         <li>Random1</li>
      </ul>
   </div>
</div>