xsl-list
[Top] [All Lists]

Re: Yet another grouping question

2004-09-16 01:27:04
Hi Shawn,

I have always found grouping to be the most frustrating so I feel your pain... What Jeni and DaveP have done such a great job of is providing a constant reference of tutorials and list content for things like grouping that with consistent reference and study will help you understand how fantastic using the Muenchian (as tough to spell as it can be to understand :) method can be to perform extremely complex transforms... Keep hitting those sites and pick up Dr. Kays book and it will all become clear at some point quite soon...

See if the following code makes sense and let me know if you need further help... Really focus on understanding your current context node.. I find breaking your XPath in smaller pieces and checking their values first before trying to compare, match, and apply styles to them accordingly really helps as well...

so, this xslt...

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:key name="index-by-primary" match="@primary" use="." />

<xsl:template match="/">
  <index>
<xsl:apply-templates select="indexRoot/indexItem[generate-id(@primary) = generate-id(key('index-by-primary', @primary))] " mode="level-one"/>
  </index>
</xsl:template>

<xsl:template match="indexItem" mode="level-one">
    <primary name="{(_at_)primary}">
      <secondary name="{(_at_)secondary}" />
<xsl:apply-templates select="following-sibling::indexItem[(_at_)primary = current()/@primary]" mode="level-two" />
    </primary>
</xsl:template>

<xsl:template match="indexItem" mode="level-two">
    <secondary name="{(_at_)secondary}" />
</xsl:template>

</xsl:stylesheet>

Will give you your desired output of:

<?xml version="1.0" encoding="UTF-8"?>
<index>
  <primary name="ide">
     <secondary name="object repository"/>
     <secondary name="Project Manager"/>
     <secondary name="Code Editor"/>
  </primary>
  <primary name="projects">
     <secondary name="type of"/>
     <secondary name="additional projects"/>
  </primary>
  <primary name="unmanaged code">
    <secondary name=""/>
  </primary>
</index>

Obviously the last "unmanaged code" doesnt have a secondary attribute but I imagine you can decide how you want to deal with that only applying the second template based on whatever criteria you might have...

Best of luck to you!

<M:D/>


Shawn McKenzie wrote:
So I've gone through the 'XSLT Questions and Answers' and Jeni's site
and it is still not clear to me why my Muenchien grouping isn't
working. Specificaly, the step related to limiting responses to the
first in the group.

Imagine I have an XML file like this:

<indexRoot>
  <indexItem primary="ide" secondary="object repository"/>
  <indexItem primary="ide" secondary="Project Manager"/>
  <indexItem primary="ide" secondary="Code Editor"/>
  <indexItem primary="projects" secondary="type of"/>
  <indexItem primary="projects" secondary="additional projects"/>
  <indexItem primary="unmanaged code"/>
</indexRoot>


I would like to group it something like:


      <index>
           <primary name="ide">
                 <secondary name="object repository"/>
                 <secondary name="Project Manager"/>
                 <secondary name="Code Editor"/>
           </primary>
           <primary name="projects">
                 <secondary name="type of"/>
                 <secondary name="additional projects"/>
           </primary>
           <primary name="unmanaged code"/>
      </index>

I have defined a key like this:

 <xsl:key name="index-by-primary" match="indexItem" use="@secondary" />

When I try to get the first with something like:

 <xsl:for-each select="indexItem[count(. | key('index-by-primary',
@primary)[1]) = 1]">
    <xsl:message>
       <xsl:value-of select="@primary"/>, <xsl:value-of select="@secondary"/>
    </xsl:message>
  </xsl:for-each>

I get a message for every indexItem, not just the first of each unique
@secondary.

--+------------------------------------------------------------------
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>