[xsl] Grouping Problem - XML Repeating Elements w/out Attributes

Subject: [xsl] Grouping Problem - XML Repeating Elements w/out Attributes
From: Brent Solly <ultra@xxxxxxxxx>
Date: Wed, 30 Jul 2008 04:43:58 -0700 (PDT)
Thank you , It works nicely :)  

This is the complete working stylesheet with the xml.  This stylesheet locates single or multiple <category> elements within an <item> element, and lists <title> elements associated with each <category> element.  If an <item> element has more than one <category> element, that means that the <title> falls under more than one <category>.  

I don't feel comfortable with the way the xml is generated with multiple categories under an <item> element along with the <title> element.  The result tree implies, to me anyway, that the <title> element is a child node of the <category> element and it really isn't.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:strip-space elements="*" />
<xsl:output method="html" encoding="UTF-8"/>
<xsl:key name="cat_name" match="category" use="."
/>

<!--Games -->
<xsl:template name="body" match="/">
    <xsl:choose>
	     <xsl:when test="rss/channel/generator =
'Systems'">
<!--Generates a unique id based on category name-->
<xsl:for-each select="//category[generate-id() = generate-id(key('cat_name',.)[1])]">
  <xsl:value-of select="." /><br/>
    <xsl:for-each select="key('cat_name',.)">
       <a href="#{generate-id(parent::item)}">
        <xsl:value-of select="../title" /><br/>
       </a>
    </xsl:for-each>
</xsl:for-each>
 <!-- Generates Table of Contents with content links-->
     <br/><br/>
           <!-- Description - Builds anchor links based on title -->
	 		<xsl:for-each select="rss/channel/item">
            <xsl:variable name="link-path" select="title"/>
            <b class="abbott_hl2"><xsl:value-of select="title"/></b><br/>
            <xsl:if test="description!=''">
            <a name="{generate-id()}"/>
              <xsl:value-of select="description"/><xsl:text>   </xsl:text>
              <a href="{$link-path}" target="_blank" class="abbott_storyurl">Click here for
fulltext</a><br/><br/>
            </xsl:if>
            </xsl:for-each>   
          <!-- End Description -->
  </xsl:when>

  </xsl:choose>
</xsl:template>
</xsl:stylesheet>

<?xml version="1.0" encoding="UTF-8"?>
<rss>
   <channel>
      <generator>Systems</generator>

      <item>
         <title>Ogre Battle is sold out</title>

         <description>There is a high demand for this rare game. It was made for the SNES and Nintendo 64. This game was unique in that your ending was based on your gameplay. You chose to progress as evil or good. But you could not be too good or too evil.</description>

         <category>SNES</category>

         <category>SEGA Genesis</category>
      </item>

      <item>
         <title>Final Fantasy 3 Breaks FF2 Record</title>

         <description>It was thought impossible to outdo FF2 gameplay. Square basically used the old engine from FF2 and enhanced it in FF3. There was the option for two players to play simultaneously against monsters.</description>

         <category>SNES</category>
      </item>

      <item>
         <title>SNES is the Ultimate System</title>

         <description>This new system has revolutionized controller design. There may have been another system with the 4 button setup on the right hand side surface of the game controller with 2 top buttons to make it 6 in all. The square edges were rounded off to provide a more comfortable experience.</description>

         <category>SNES</category>
      </item>

      <item>
         <title>Sonic The Hedgehog an instant success</title>

         <description>A new Mario is been born. It seems that the music on the Genesis was a notch a better than the SNES. Sonic The Hedgehog delivered in gameplay, graphics, fun factor, and sound.</description>

         <category>SEGA Genesis</category>
      </item>

      <item>
         <title>Joe Montanna Talking Football is amazing</title>

         <description>Listen play by play in this new form of football game. Play-by-Play commentary was a pleasant surprise for gamers who were used to blurry one-liners every 15 minutes. The side view made it feel just like NFL Sunday.</description>

         <category>SEGA Genesis</category>
      </item>
   </channel>
</rss>



> Date: Tue, 29 Jul 2008 16:42:35 +0200
> To:  xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> From: Martin Honnen <Martin.Honnen@xxxxxx>
> Subject: Re: [xsl] Re: Grouping Problem - XML Repeating Elements w/out Attributes
> Message-ID: <488F2C5B.3010202@xxxxxx>
> 
> Brent Solly wrote:
> 
> > <xsl:for-each select="//category[generate-id()
> = generate-id(key('cat_name',.)[1])]">
> >   <xsl:value-of select="."
> /><br/>
> >     <xsl:for-each
> select="key('cat_name',.)">
> >        <a href="#{generate-id()}">
> 
> Try
>           <a
> href="#{generate-id(parent::item)}">
> here to fix the anchor problem you describe below.
> 
> > But, now the anchor links are broken.  When I click on
> a hyperlink, the output of the <title> element, it
> should take me down to the corresponding title and
> description.  
> 
> -- 
> 
> 	Martin Honnen
> 	http://JavaScript.FAQTs.com/

Current Thread