开发者

XSLT version 1.0 How to do multiple group by

开发者 https://www.devze.com 2023-02-04 09:31 出处:网络
Could anyone please help me with multiple groupings with the XSLT. I would like to group the following XML data by ORG, by fiscal year quarter (QTR2) and output it in a specific format.

Could anyone please help me with multiple groupings with the XSLT. I would like to group the following XML data by ORG, by fiscal year quarter (QTR2) and output it in a specific format.

Here is the 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>
  ...

output should look something like this:

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

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

I've also tried this:

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


I think that you haven't assembled the pieces together... Following your style:

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

Output:

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

精彩评论

暂无评论...
验证码 换一张
取 消