XSLT 版本 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<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>
这是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您还没有将各个部分组装在一起...遵循您的风格:
输出:
I think that you haven't assembled the pieces together... Following your style:
Output: