RE: [xsl] First occurence of a repeating value

Subject: RE: [xsl] First occurence of a repeating value
From: Jarno.Elovirta@xxxxxxxxx
Date: Thu, 7 Nov 2002 09:19:33 +0200
Hi,

> My XML is of the form:
> 
> <main>
>   <sub>
>      <entry>
>         <item1>val1</item1>
>         <item2>val2</item2>
>         <item3>val3</item3>
>      </entry>
>      <entry>
>         <item1>val4</item1>
>         <item2>val2</item2>
>         <item3>val5</item3>
>      </entry>
>      <entry>
>         <item1>val6</item1>
>         <item2>val2</item2>
>         <item3>val7</item3>
>      </entry>
>    </sub>
> </main>
> 
> I want the output to be as shown below, using "val2" once as 
> the title,
> and listing all the other values after that:
> 
> Title: val2
> ------
> Item1: val1
> Item3: val3
> 
> Item4: val4
> Item5: val5
> 
> Item6: val6
> Item7: val7
> 
> Any suggestions on how to accomplish this? Thanks in advance.

For example,

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:apply-templates select="main/sub"/>
  </xsl:template>

  <xsl:template match="sub">
    <xsl:text />Title: <xsl:value-of select="entry/item2"/>
    <xsl:text>&#xA;------&#xA;</xsl:text>
    <xsl:for-each select="entry/*[not(self::item2)][position() mod 2 = 1]">
      <xsl:for-each select=". | following-sibling::*[not(self::item2)]">
        <xsl:text/>Item<xsl:value-of select="substring(., string-length(.))"/>: <xsl:value-of select="."/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>

Cheers,

Jarno

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


Current Thread