Flat puzzle. Replacing key() with pipe agian.

Subject: Flat puzzle. Replacing key() with pipe agian.
From: Paul Tchistopolskii <paul@xxxxxxx>
Date: Mon, 07 Aug 2000 22:04:29 -0700
 Hello.

 Thank you Eric, Sebastian, *of course*  Steve and many other 
 subscribers of this list.

 The next huge letter is a brief explanation of what is XSLT from 
 my point  of view.

 First - the solution of 'Flat puzzle' without key().
 *Exactly* the same pattern has used as it was for test6.xsl.
 
 Step1. QUERY.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="xml" indent="yes"/>

  <xsl:template match="LIST">
  <doc>
        <xsl:for-each select="*[not(@dep)]">
  <xsl:call-template name="gen-group">
   <xsl:with-param name="grp-name" select="name()"/>
   <xsl:with-param name="cur-elem-name" select="name()"/>
   <xsl:with-param name="list" select="/LIST/*"/>
  </xsl:call-template>
        </xsl:for-each>
  </doc>
  </xsl:template>

  <xsl:template name="gen-group">
 <xsl:param name="grp-name"/>
 <xsl:param name="cur-elem-name"/>
 <xsl:param name="list"/>

 <element>
         <group> <xsl:value-of select="$grp-name"/> </group>
  <name> <xsl:value-of select="$cur-elem-name"/> </name>
 </element>
 
 <!-- Who depends on this element? -->

 <xsl:for-each select="$list[ @dep = $cur-elem-name ]">
 
  <xsl:call-template name="gen-group">
   <xsl:with-param name="grp-name" select="$grp-name"/>
   <xsl:with-param name="cur-elem-name" select="name()"/>
   <xsl:with-param name="list" select="$list"/>
  </xsl:call-template>

 </xsl:for-each>

  </xsl:template>

</xsl:stylesheet>


 Produces:

<?xml version="1.0" encoding="utf-8"?>
<doc>
   <element>
      <group>A</group>
      <name>A</name>
   </element>
   <element>
      <group>A</group>
      <name>B</name>
   </element>
   <element>
      <group>A</group>
      <name>C</name>
   </element>
   <element>
      <group>A</group>
      <name>Q</name>
   </element>
   <element>
      <group>D</group>
      <name>D</name>
   </element>
   <element>
      <group>X</group>
      <name>X</name>
   </element>
   <element>
      <group>X</group>
      <name>Y</name>
   </element>
   <element>
      <group>X</group>
      <name>Z</name>
   </element>
</doc>

Step 2. RENDER. Similiar to step2.xsl of test6.  
This is universal mechanizm for flat-> hierarhy.
See comments in the next letter.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
 <html>
 <body>

 <xsl:call-template name="draw-group">
  <xsl:with-param name="list" select="/doc/element"/>
 </xsl:call-template>

 </body>
 </html>
  </xsl:template>

  <xsl:template name="draw-group">
 <xsl:param name="list"/>

  <xsl:if test="$list">

 <xsl:variable name="group" select="$list[1]/group"/>
 <xsl:variable name="n_items" select="count( $list[ group = $group ] )"/>
 <xsl:variable name="cur-grp" select="$list[ not ( position() &gt; $n_items ) ]"/>
 <xsl:variable name="rest"  select="$list[ position() &gt; $n_items ]"/>

 <xsl:variable name="title">
  <xsl:for-each select="$cur-grp/name">
   <xsl:value-of select="."/>
   <xsl:text> </xsl:text>
  </xsl:for-each>
 </xsl:variable> 

 <h2> <xsl:value-of select="$title"/> </h2>

 <ol>
 <xsl:for-each select="$cur-grp">
  <li>
   <xsl:value-of select="name"/>
  </li>  
 </xsl:for-each> 
 </ol>
 
 <xsl:call-template name="draw-group">
  <xsl:with-param name="list" select="$rest"/>
 </xsl:call-template>

  </xsl:if>

  </xsl:template>

</xsl:stylesheet>




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


Current Thread