[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: for-each-loop case

Subject: RE: for-each-loop case
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 8 Apr 2005 08:24:47 +0100
loop case
> 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:

A two phase transform is often a good thing - it keeps your code modular and
reusable!
> 
> input:

It's really helpful to readers to add some indentation:
> 
> <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">
...
>   </student>
>  </devision>
>  <devision id="2">
...
>  </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>
...
>    </subject>
> </scores>
> 

An unusual one!

I think this is:

<xsl:for-each-group select="//subject" group-by="@name">
<subject name="{current-grouping-key()}">
  <xsl:variable name="students-of-subject"
select="current-group()/parent::student"/>
  <xsl:for-each-group select="current-group()/ancestor::devision"
group-by="@id">
    <devision id="{current-grouping-key()}">
      <xsl:copy-of select="$students-of-subject intersect
current-group()/student"/>
    </
  </
</
</

Not tested.

Michael Kay
http://www.saxonica.com/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.