This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: 2 level Grouping through attributes


At 2002-02-11 09:35 +0200, Yoav Broudo wrote:
>I am trying to achieve multi level grouping (actually two levels) of the
>next XML.

I find the use of variables makes multi-level grouping of any depth quite easy.

>How can I achieve this?

I hope the example below helps.  Note how I create a variable at each level 
of only those in the subgroup, making it quite straightforward.

I hope this helps.

.................Ken

T:\ftemp>type yoav.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<ROOT>
<XML_OUTPUT ID="1" UserID="3" UserName="Mark" CategoryID="1" 
CategoryTitle="Category A" ContentItemID="1" ItemTitle="Title 1"/>
<XML_OUTPUT ID="2" UserID="3" UserName="Mark" CategoryID="5" 
CategoryTitle="Category B" ContentItemID="45" ItemTitle="Title 45"/>
<XML_OUTPUT ID="3" UserID="3" UserName="Mark" CategoryID="5" 
CategoryTitle="Category B" ContentItemID="10" ItemTitle="Title 10"/>
<XML_OUTPUT ID="4" UserID="3" UserName="Mark" CategoryID="5" 
CategoryTitle="Category B" ContentItemID="11" ItemTitle="Title 11"/>
<XML_OUTPUT ID="5" UserID="3" UserName="Mark" CategoryID="5" 
CategoryTitle="Category B" ContentItemID="12" ItemTitle="Title 12"/>
<XML_OUTPUT ID="10" UserID="4" UserName="Joe" CategoryID="6" 
CategoryTitle="Category C" ContentItemID="16" ItemTitle="Title 16"/>
<XML_OUTPUT ID="11" UserID="4" UserName="Joe" CategoryID="6" 
CategoryTitle="Category C" ContentItemID="13" ItemTitle="Title 13"/>
<XML_OUTPUT ID="12" UserID="4" UserName="Joe" CategoryID="6" 
CategoryTitle="Category C" ContentItemID="15" ItemTitle="Title 15"/>
<XML_OUTPUT ID="13" UserID="4" UserName="Joe" CategoryID="10" 
CategoryTitle="Category E" ContentItemID="18" ItemTitle="Title 18"/>
<XML_OUTPUT ID="14" UserID="4" UserName="Joe" CategoryID="10" 
CategoryTitle="Category E" ContentItemID="19" ItemTitle="Title 19"/>
</ROOT>

T:\ftemp>type yoav.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                 version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
   <ROOT>
     <xsl:variable name="users" select="ROOT/XML_OUTPUT"/>
     <xsl:for-each select="$users">
       <xsl:if test="generate-id(.)=
                     generate-id($users[@UserID=current()/@UserID])">
         <USER ID="{@UserID}" NAME="{@UserName}">
           <xsl:variable name="categories"
                         select="$users[@UserID=current()/@UserID]"/>
           <xsl:for-each select="$categories">
             <xsl:if test="generate-id(.)=
                           generate-id($categories[@CategoryID=
                                                   current()/@CategoryID])">
               <CATEGORY ID="{@CategoryID}" TITLE="{@CategoryTitle}">
                 <xsl:for-each select="$categories[@CategoryID=
                                                   current()/@CategoryID]">
                   <CONTENT_ITEM ID="{@ContentItemID}"
                                 TITLE="{@ItemTitle}"/>
                 </xsl:for-each>
               </CATEGORY>
             </xsl:if>
           </xsl:for-each>
         </USER>
       </xsl:if>
     </xsl:for-each>
   </ROOT>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt yoav.xml yoav.xsl
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
<USER ID="3" NAME="Mark">
<CATEGORY ID="1" TITLE="Category A">
<CONTENT_ITEM ID="1" TITLE="Title 1"/>
</CATEGORY>
<CATEGORY ID="5" TITLE="Category B">
<CONTENT_ITEM ID="45" TITLE="Title 45"/>
<CONTENT_ITEM ID="10" TITLE="Title 10"/>
<CONTENT_ITEM ID="11" TITLE="Title 11"/>
<CONTENT_ITEM ID="12" TITLE="Title 12"/>
</CATEGORY>
</USER>
<USER ID="4" NAME="Joe">
<CATEGORY ID="6" TITLE="Category C">
<CONTENT_ITEM ID="16" TITLE="Title 16"/>
<CONTENT_ITEM ID="13" TITLE="Title 13"/>
<CONTENT_ITEM ID="15" TITLE="Title 15"/>
</CATEGORY>
<CATEGORY ID="10" TITLE="Category E">
<CONTENT_ITEM ID="18" TITLE="Title 18"/>
<CONTENT_ITEM ID="19" TITLE="Title 19"/>
</CATEGORY>
</USER>
</ROOT>

T:\ftemp>

--
Upcoming: 3-days XSLT/XPath and/or 2-days XSLFO - Feb 18-22, 2002

G. Ken Holman                mailto:gkholman@CraneSoftwrights.com
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                        Definitive XSLT & XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:   2002-02-13,14,15,18,21,03-04,05,06,11,15,
-        04-08,09,10,11,05-06,07,09,10,14,15,06-04,07,10,11,13,14


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]