[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] matching the first following sibling


Subject: Re: [xsl] matching the first following sibling
From: Biying Huang <biying.huang@xxxxxxxxxxxxxxx>
Date: Thu, 22 Jan 2004 12:32:07 -0500

Thanks a lot to Lars and Kakridge. Now I learn more XSLT.

Biying


Lars Huttar wrote:

> >
> > Dear All,
> >
> > I have trouble with matching the first following sibling which has a
> > child's value differ from the current one's.
> >
> > I need my output look like this:
> >
> > 3000: 9
> > 5000: 5
> > 4000: 8
> >
> > But I can only get the first line,  3000 : 9
> >
> > Here is my xslt:
> >
> > <xsl:template match="ProductList">
> >     <xsl:apply-templates select="Substance[position() = 1 ]" />
> > </xsl:template>
> >
> > <xsl:template match="Substance">
> >   <xsl:value-of select = "productId"/>
> >    :
> >   <xsl:value-of select = "sum( num  |
> > following-sibling::Substance[productId = $productId ]/num)" />
>
> I assume you're defining the variable productId to be the value
> of the productId child that is matched by the template
> (i.e. the value produced by the first xsl:value-of).
>
> >   <!--  now check if there is another unique product, if so, call this
> > template recursively, but this DOES NOT work   -->
> >   <xsl:apply-templates select="following-sibling::Substance[productId
> > !=  ./productId and position() = 1 ]" />
>
> Part of the problem here is that "./productId" is the same as "productId".
> Both are evaluated in the context of the following-sibling::Substance
> nodes. What you probably meant was
>    <xsl:apply-templates
>         select="following-sibling::Substance
>                 [productId !=  current()/productId and position() = 1 ]" />
>
> Above where you used $productId, you have the same issue.
> In both cases you could either use current()/productId, or
> define a productId variable (once) and use it (both times).
>
> (But yes, kakridge's solution is cleaner and scales much better.)
>
> Lars
>
> > </xsl:template>
> >
> >
> > I have this xml data:
> >
> > <ProductList>
> >   <Substance>
> >       <productId>3000</productId>
> >       <num>3</num>
> >    </Substance>
> >   <Substance>
> >       <productId>4000</productId>
> >       <num>4</num>
> >    </Substance>
> >   <Substance>
> >       <productId>3000</productId>
> >       <num>3</num>
> >    </Substance>
> >   <Substance>
> >       <productId>5000</productId>
> >       <num>5</num>
> >    </Substance>
> >   <Substance>
> >       <productId>4000</productId>
> >       <num>4</num>
> >    </Substance>
> >   <Substance>
> >       <productId>3000</productId>
> >       <num>3</num>
> >    </Substance>
> >  </ProductList>
> >
> >
> >
> > Thanks in advance for your help.
> >
> > Biying Huang
> >
> >
> >  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> >
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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



Current Thread
Keywords
xml