xsl-list
[Top] [All Lists]

for-each-loop case

2005-04-07 22:50:12
Hi Folks,

I am pretty new to XSLT2.0. My question is how to get desired output
xml from following input by just using for-each-group. I can do it now
with temporary tree but that means two phases transform:

input:

<scores>
<devision id="1">
<student name="Tiger">
<subject name="English">
<score>98</score>
</subject>
<subject name="Math">
<score>102</score>
</subject>
<subject name="History">
<score>100</score>
</subject>
</student>
<student name="Jack">
<subject name="English">
<score>88</score>
</subject>
<subject name="Math">
<score>92</score>
</subject>
<subject name="History">
<score>90</score>
</subject>
</student>
</devision>
<devision id="2">
<student name="Phil">
<subject name="English">
<score>108</score>
</subject>
<subject name="Math">
<score>112</score>
</subject>
<subject name="History">
<score>110</score>
</subject>
</student>
<student name="Ernie">
<subject name="English">
<score>118</score>
</subject>
<subject name="Math">
<score>122</score>
</subject>
<subject name="History">
<score>120</score>
</subject>
</student>
</devision>
</scores>

Desired output :

<?xml version="1.0" encoding="UTF-8"?>
<scores>
   <subject name="English">
      <devision id="1">
         <student name="Tiger">
            <score>98</score>
         </student>
         <student name="Jack">
            <score>88</score>
         </student>
      </devision>
      <devision id="2">
         <student name="Phil">
            <score>108</score>
         </student>
         <student name="Ernie">
            <score>118</score>
         </student>
      </devision>
   </subject>
   <subject name="Math">
      <devision id="1">
         <student name="Tiger">
            <score>102</score>
         </student>
         <student name="Jack">
            <score>92</score>
         </student>
      </devision>
      <devision id="2">
         <student name="Phil">
            <score>112</score>
         </student>
         <student name="Ernie">
            <score>122</score>
         </student>
      </devision>
   </subject>
   <subject name="History">
      <devision id="1">
         <student name="Tiger">
            <score>100</score>
         </student>
         <student name="Jack">
            <score>90</score>
         </student>
      </devision>
      <devision id="2">
         <student name="Phil">
            <score>110</score>
         </student>
         <student name="Ernie">
            <score>120</score>
         </student>
      </devision>
   </subject>
</scores>

Here is my current template:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
<xsl:output method="xml" indent="yes"/> 

<xsl:template match="/">
<scores>
<xsl:apply-templates select="$intermediate" mode="phase2"/>
</scores>
</xsl:template>
<xsl:variable name="intermediate">
<score>
<xsl:apply-templates select="scores" mode="phase1"/>
</score>
</xsl:variable>
<xsl:template match="score" mode="phase2">
<xsl:for-each-group select="subject" group-by="@name">
<subject name="{(_at_)name}">
<xsl:for-each-group select="current-group()" group-by="devision/@id">
<devision id="{devision/@id}">
      <xsl:for-each select="current-group()">
      <xsl:copy-of select="devision/student"/>
      </xsl:for-each>
</devision>
</xsl:for-each-group>
</subject>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="scores" mode="phase1">
<xsl:for-each select="devision/student/subject">
<subject name="{(_at_)name}">
<devision id="{../../@id}">
      <student name="{../@name}">
  <score><xsl:value-of select="score" /></score>
      </student>
</devision>
</subject>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Thanks in adavance. 

Bridger

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>