XSLT 版本 1.0 如何进行多个分组依据

发布于 2024-10-11 18:57:07 字数 5124 浏览 1 评论 0原文

谁能帮我用 XSLT 进行多个分组。 我想按 ORG、按财政年度季度 (QTR2) 对以下 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>

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<br>FY10" y="11"/>
  <point name="QTR2<br>FY10" y="13"/>
  <point name="QTR4<br>FY10" y="50"/>
  <point name="QTR1<br>FY11" y="9"/>
  <point name="QTR2<br>FY11" y="1"/>
 </series>
 <series name="00B">
  <point name="QTR1<br>FY10" y="10"/>
  <point name="QTR2<br>FY10" y="4"/>
  <point name="QTR3<br>FY10" y="7"/>
  <point name="QTR1<br>FY11" y="9"/>
  <point name="QTR2<br>FY11" y="2"/>
 </series>
 <series name="00C">
  <point name="QTR1<br>FY10" y="7"/>
  <point name="QTR2<br>FY10" y="21"/>
  <point name="QTR3<br>FY10" y="4"/>
  <point name="QTR4<br>FY10" y="5"/>
  <point name="QTR1<br>FY11" y="11"/>
  <point name="QTR2<br>FY11" y="13"/>
 </series>
 <series name="00T">
  <point name="QTR1<br>FY10" y="14"/>
  <point name="QTR2<br>FY10" y="17"/>
  <point name="QTR3<br>FY10" y="20"/>
  <point name="QTR4<br>FY10" y="5"/>
  <point name="QTR2<br>FY11" y="18"/>
 </series>
 <series name="00">
  <point name="QTR1<br>FY10" y="2"/>
  <point name="QTR2<br>FY10" y="19"/>
  <point name="QTR3<br>FY10" y="6"/>
  <point name="QTR4<br>FY10" y="13"/>
  <point name="QTR1<br>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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2024-10-18 18:57:07

我认为您还没有将各个部分组装在一起...遵循您的风格:

<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><br/></xsl:text>
    </xsl:template>
</xsl:stylesheet>

输出:

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

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><br/></xsl:text>
    </xsl:template>
</xsl:stylesheet>

Output:

<Data>
    <series name="00">
        <point y="2" name="FY10 2nd QTR<br/>(1 JAN - 31 MAR)"></point>
    </series>
    <series name="00A">
        <point y="11" name="FY10 1st QTR<br/>(1 OCT - 31 DEC)"></point>
        <point y="13" name="FY10 2nd QTR<br/>(1 JAN - 31 MAR)"></point>
    </series>
    <series name="00B">
        <point y="4" name="FY10 2nd QTR<br/>(1 JAN - 31 MAR)"></point>
    </series>
    <series name="00C">
        <point y="2" name="FY10 1st QTR<br/>(1 OCT - 31 DEC)"></point>
    </series>
    <series name="00T">
        <point y="11" name="FY10 1st QTR<br/>(1 OCT - 31 DEC)"></point>
    </series>
</Data>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文