Re: [xsl] for-each-group dropping one element (my inability to structure a test correctly)

Subject: Re: [xsl] for-each-group dropping one element (my inability to structure a test correctly)
From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
Date: Sun, 7 Feb 2010 09:41:23 +0100
Not being sure what your requirements are, I've found that this change, as
close to the original solution, does the trick:

           ...

           <xsl:copy-of select="Location/Element" copy-namespaces="no"/>

           <Description>
             <xsl:for-each select="current-group()">

               <xsl:if
test="not(Location/Element/following-sibling::SubDiv)">
                 <xsl:copy-of select="Data" copy-namespaces="no"/>
               </xsl:if>
             </xsl:for-each>
           </Description>


           <xsl:for-each-group select="current-group()"
group-by="Location/SubDiv">
           ...
-W

On Sun, Feb 7, 2010 at 9:09 AM, Mark Wilson <mark@xxxxxxxxxxxx> wrote:
>
> Hi all,
> I have been at this for about six hours.
>
> As written, the style sheet does not produce the element marked "Missing
element" in Listing 3 (the output ) and as "Not output" in Listing one (source
document). The problem is marked "My problem" in the Listing 2 (style sheet).
>
> I Know that my problem is that I am telling the style sheet to give me only
the first <Data> element. What I don't know is how to tell it to give me all
of the <Data> elements that meet the <xsl:if> test, but none of the <Data>
element that do not meet that test. Or have I just set everything up wrong to
start with?
>
> Any help would be appreciated
> Mark
>
> Listing 1: Input
> <?xml version="1.0" encoding="UTF-8"?>
> <List >
>   <Item plate="1" position="1">
>       <Feature>
>           <Location>
>               <Element>rays</Element>
>           </Location>
>           <Data>dot below the rays under TA</Data>
>       </Feature>
>       <Feature>
>           <Location>
>               <Element>rays</Element>
>               <SubDiv>main</SubDiv>
>           </Location>
>           <Data>main ray swollen at both ends</Data>
>       </Feature>
>   </Item>
>   <Item plate="2" position="1">
>       <Feature>
>           <Location>
>               <Element>rays</Element>
>           </Location>
>           <Data>dot below the rays under S</Data> <!-- Not output -->
>       </Feature>
>   </Item>
>   <Item plate="1" position="2">
>       <Feature>
>           <Location>
>               <Element>rays</Element>
>               <SubDiv>main</SubDiv>
>           </Location>
>           <Data>main ray thicker in the middle</Data>
>       </Feature>
>   </Item>
> </List>
>
> Listing 2: Style Sheet
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xs="http://www.w3.org/2001/XMLSchema"; exclude-result-prefixes="xs"
version="2.0">
>
>   <xsl:template match="List">
>       <List>
>           <xsl:for-each-group select="Item/Feature"
group-by="Location/Element">
>               <xsl:sort select="current-grouping-key()"/>
>               <Item>
>                   <Feature>
>                       <xsl:copy-of select="Location/Element"
copy-namespaces="no"/>
>
>                       <xsl:if
test="not(Location/Element/following-sibling::SubDiv)">
>                           <Description>
>                                   <xsl:copy-of select="Data"
copy-namespaces="no"/><!-- My Problem -->
>                              </Description>
>                       </xsl:if>
>
>                       <xsl:for-each-group select="current-group()"
group-by="Location/SubDiv">
>                           <xsl:sort select="current-grouping-key()"/>
>                           <Level1>
>                           <xsl:copy-of select="Location/SubDiv"
copy-namespaces="no"/>
>                           <Description>
>                           <xsl:copy-of select="current-group()/Data"
copy-namespaces="no"/>
>                           </Description>
>                           </Level1>
>                       </xsl:for-each-group>
>                   </Feature>
>               </Item>
>           </xsl:for-each-group>
>       </List>
>   </xsl:template>
> </xsl:stylesheet>
>
> Listing 3: Output
> <?xml version="1.0" encoding="UTF-8"?>
> <List>
>   <Item>
>       <Feature>
>           <Element>rays</Element>
>           <Description>
>               <Data>dot below the rays under TA</Data>
> <!-- Missing Element: <Data>dot below the rays under S</Data> goes here -->
>           </Description>
>           <Level1>
>               <SubDiv>main</SubDiv>
>               <Description>
>                   <Data>main ray swollen at both ends</Data>
>                   <Data>main ray thicker in the middle</Data>
>               </Description>
>           </Level1>
>       </Feature>
>   </Item>
> </List>

Current Thread