XSLT 1.0版如何做多个组

时间:2011-01-11 16:48:27

标签: xslt xslt-1.0

有没有人可以帮我多次使用XSLT进行分组。 我希望按财政年度季度(QTR2)将ORG的以下XML数据分组,并以特定格式输出。

这是XML:

<NewDataSet>
 <Data>
  <ORG>00A</ORG> 
  <TASK_COUNT>11</TASK_COUNT> 
  <FY>10</FY> 
  <QTR>1st QTR-FY10</QTR> 
  <QTR2>FY10 1st QTR <br>(1 OCT - 31 DEC)</QTR2> 
  </Data>
 <Data>
  <ORG>00C</ORG> 
  <TASK_COUNT>2</TASK_COUNT> 
  <FY>10</FY> 
  <QTR>1st QTR-FY10</QTR> 
  <QTR2>FY10 1st QTR <br>(1 OCT - 31 DEC)</QTR2> 
  </Data>
 <Data>
  <ORG>00T</ORG> 
  <TASK_COUNT>11</TASK_COUNT> 
  <FY>10</FY> 
  <QTR>1st QTR-FY10</QTR> 
  <QTR2>FY10 1st QTR <br>(1 OCT - 31 DEC)</QTR2> 
  </Data>
 <Data>
  <ORG>00</ORG> 
  <TASK_COUNT>2</TASK_COUNT> 
  <FY>10</FY> 
  <QTR>2nd QTR-FY10</QTR> 
  <QTR2>FY10 2nd QTR <br>(1 JAN - 31 MAR)</QTR2> 
  </Data>
 <Data>
  <ORG>00A</ORG> 
  <TASK_COUNT>13</TASK_COUNT> 
  <FY>10</FY> 
  <QTR>2nd QTR-FY10</QTR> 
  <QTR2>FY10 2nd QTR <br>(1 JAN - 31 MAR)</QTR2> 
  </Data>
 <Data>
  <ORG>00B</ORG> 
  <TASK_COUNT>4</TASK_COUNT> 
  <FY>10</FY> 
  <QTR>2nd QTR-FY10</QTR> 
  <QTR2>FY10 2nd QTR <br>(1 JAN - 31 MAR)</QTR2> 
  </Data>
  ...

输出应该如下所示:

<data>
 <series name="00A">
  <point name="QTR1&lt;br&gt;FY10" y="11"/>
  <point name="QTR2&lt;br&gt;FY10" y="13"/>
  <point name="QTR4&lt;br&gt;FY10" y="50"/>
  <point name="QTR1&lt;br&gt;FY11" y="9"/>
  <point name="QTR2&lt;br&gt;FY11" y="1"/>
 </series>
 <series name="00B">
  <point name="QTR1&lt;br&gt;FY10" y="10"/>
  <point name="QTR2&lt;br&gt;FY10" y="4"/>
  <point name="QTR3&lt;br&gt;FY10" y="7"/>
  <point name="QTR1&lt;br&gt;FY11" y="9"/>
  <point name="QTR2&lt;br&gt;FY11" y="2"/>
 </series>
 <series name="00C">
  <point name="QTR1&lt;br&gt;FY10" y="7"/>
  <point name="QTR2&lt;br&gt;FY10" y="21"/>
  <point name="QTR3&lt;br&gt;FY10" y="4"/>
  <point name="QTR4&lt;br&gt;FY10" y="5"/>
  <point name="QTR1&lt;br&gt;FY11" y="11"/>
  <point name="QTR2&lt;br&gt;FY11" y="13"/>
 </series>
 <series name="00T">
  <point name="QTR1&lt;br&gt;FY10" y="14"/>
  <point name="QTR2&lt;br&gt;FY10" y="17"/>
  <point name="QTR3&lt;br&gt;FY10" y="20"/>
  <point name="QTR4&lt;br&gt;FY10" y="5"/>
  <point name="QTR2&lt;br&gt;FY11" y="18"/>
 </series>
 <series name="00">
  <point name="QTR1&lt;br&gt;FY10" y="2"/>
  <point name="QTR2&lt;br&gt;FY10" y="19"/>
  <point name="QTR3&lt;br&gt;FY10" y="6"/>
  <point name="QTR4&lt;br&gt;FY10" y="13"/>
  <point name="QTR1&lt;br&gt;FY11" y="11"/>
 </series>
</data>

这是XSLT:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml"  omit-xml-declaration="yes"/>
 <xsl:key name="byORG" match="Data" use="ORG"/>
 <xsl:key name="byORGbyQTR2" match="Data" use="concat(ORG, '|', QTR2)"/>
 <xsl:template match="/">
  <Data>
   <xsl:apply-templates select="NewDataSet/Data[generate-id() = generate-id(key('byORG', ORG)[1])]">
    <xsl:sort select="ORG"/>
   </xsl:apply-templates>
  </Data>
 </xsl:template>
 <xsl:template match="Data">
  <xsl:apply-templates select="key('byORG', ORG)[generate-id() = generate-id(key('byORGbyQTR2', concat(ORG, '|', QTR2))[1])]" mode="qrt2">
   <xsl:sort select="QTR2"/>
  </xsl:apply-templates>
 </xsl:template>
</xsl:stylesheet>

我也试过这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" />
 <xsl:key match="Data" name="group-by-org" use="ORG"></xsl:key>
 <xsl:template match="/">
  <Data>
   <xsl:for-each select="NewDataSet/Data[key('group-by-org', ORG)]">
    <series>
     <xsl:attribute name="name">
      <xsl:value-of select="ORG"/>
     </xsl:attribute>
     <point>
      <xsl:attribute name="name">
      <xsl:value-of select="QTR2"/>
     </xsl:attribute>
      <xsl:attribute name="y">
      <xsl:value-of select="TASK_COUNT"/>
     </xsl:attribute>
     </point>
    </series>
   </xsl:for-each>
  </Data>
 </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:2)

我认为你还没有将这些作品组合在一起...... 遵循你的风格:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"  omit-xml-declaration="yes"/>
    <xsl:key name="byORG" match="Data" use="ORG"/>
    <xsl:key name="byORGbyQTR2" match="Data" use="concat(ORG, '|', QTR2)"/>
    <xsl:template match="/">
        <Data>
            <xsl:apply-templates
                 select="NewDataSet/Data[generate-id() =
                                         generate-id(key('byORG', ORG)[1])]">
                <xsl:sort select="ORG"/>
            </xsl:apply-templates>
        </Data>
    </xsl:template>
    <xsl:template match="Data">
        <series name="{ORG}">
            <xsl:apply-templates
                 select="key('byORG', ORG)
                            [generate-id() =
                             generate-id(key('byORGbyQTR2',
                                             concat(ORG, '|', QTR2))[1])]"
                 mode="qrt2">
                <xsl:sort select="QTR2"/>
            </xsl:apply-templates>
        </series>
    </xsl:template>
    <xsl:template match="Data" mode="qrt2">
        <point y="{TASK_COUNT}">
            <xsl:attribute name="name">
                <xsl:apply-templates select="QTR2"/>
            </xsl:attribute>
        </point>
    </xsl:template>
    <xsl:template match="br">
        <xsl:text>&lt;br/&gt;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

输出:

<Data>
    <series name="00">
        <point y="2" name="FY10 2nd QTR&lt;br/&gt;(1 JAN - 31 MAR)"></point>
    </series>
    <series name="00A">
        <point y="11" name="FY10 1st QTR&lt;br/&gt;(1 OCT - 31 DEC)"></point>
        <point y="13" name="FY10 2nd QTR&lt;br/&gt;(1 JAN - 31 MAR)"></point>
    </series>
    <series name="00B">
        <point y="4" name="FY10 2nd QTR&lt;br/&gt;(1 JAN - 31 MAR)"></point>
    </series>
    <series name="00C">
        <point y="2" name="FY10 1st QTR&lt;br/&gt;(1 OCT - 31 DEC)"></point>
    </series>
    <series name="00T">
        <point y="11" name="FY10 1st QTR&lt;br/&gt;(1 OCT - 31 DEC)"></point>
    </series>
</Data>