xsl-list
[Top] [All Lists]

[xsl] Is there a way to sometimes bypass a nested for-each group?

2008-07-24 00:57:18
Hi,
The style sheet in Listing 2 is a modification of the nested for-each-group example in Michael Kay's 4th ed. When a <SubHeading> element is present, all goes well, but when there is no <SubHeading>, the sequence constructor following the nested for-each-group is, of course, not executed. Any <Heading> that gets grouped with <Items> where some have a <SubHeading> and others do not, fails to execute the nested sequence constructor.

I tried an xsl:choose construct testing for a <SubHeading> and branching around the nested for-each group [putting the xsl:sorts and <Article> sequence constructor in both paths], but could not make that work either. This time, any <Heading> that gets grouped with <Items> where some have a <SubHeading> and others do not, fails to produce the <SubHeading> when it is present but did execute the sequence constructors plated in both paths. I have a clue what is wrong (the groups), but not a clue about fixing it.
Thanks,
Mark


Listing 1: data.
<List>
   <Item>
       <Heading entry="subject">Catalogs</Heading>
       <SubHeading>Czechoslovakia</SubHeading>
       <Article>
           <Title>2007 - Ceskoslovensko 1918-1939 -POFIS</Title>
           <Person>Benes, Frantisek.</Person>
           <Person>Horvath, Savoy [reviewer]</Person>
           <IssueName>Nov/Dec</IssueName>
           <Year>2007</Year>
           <IssueNumber>6</IssueNumber>
           <Page>22</Page>
       </Article>
   </Item>
   <Item>
       <Heading entry="subject">Catalogs</Heading>
       <Article>
<Title>Comparison survey of catalog numbers of Czechoslovak postage stamps (cont)</Title>
           <Person>Horvath, Savoy</Person>
           <IssueName>Jul/Aug</IssueName>
           <Year>2003</Year>
           <IssueNumber>4</IssueNumber>
           <Page>18</Page>
       </Article>
   </Item>
</List>

Listing 2: style sheet
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output method="html" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="List">
   <List>
     <!-- There is always a Heading element -->

     <xsl:for-each-group select="Item" group-by="Heading">
       <xsl:sort select="current-grouping-key()"/>
       <Item>
         <xsl:element name="Heading">
           <xsl:attribute name="entry">
             <xsl:value-of select="Heading/@entry"/>
           </xsl:attribute>
           <xsl:value-of select="current-grouping-key()"/>
         </xsl:element>

<!-- Here is my nesting problem: the SubHeading element may not always be present -->

<xsl:for-each-group select="current-group()" group-by="SubHeading">
               <xsl:sort select="current-grouping-key()"/>
               <xsl:element name="SubHeading">
                 <xsl:value-of select="current-grouping-key()"/>
               </xsl:element>

<!-- the following sequence constructor needs to be executed whether or not there is a <SubHeading> -->

             <xsl:for-each select="current-group()">
             <xsl:sort select="Title"/>
             <xsl:sort select="Year"/>
             <xsl:sort select="IssueNumber"/>
             <xsl:sort select="Page"/>
             <Article>
               <xsl:apply-templates/>
             </Article>
           </xsl:for-each>
            </xsl:for-each-group>
       </Item>
      </xsl:for-each-group>
   </List>
 </xsl:template>

<!-- Pass these through -->
 <xsl:template match="Title"><xsl:copy-of select="."/></xsl:template>
 <xsl:template match="Year"><xsl:copy-of select="."/></xsl:template>
  <xsl:template match="IssueName"><xsl:copy-of select="."/></xsl:template>
 <xsl:template match="Person"><xsl:copy-of select="."/></xsl:template>
 <xsl:template match="Page"><xsl:copy-of select="."/></xsl:template>

<!-- Discard or reformat these -->
 <xsl:template match="Heading "/>
 <xsl:template match="SubHeading" />
 <xsl:template match="IssueNumber"/>

</xsl:stylesheet>


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