.Re: Grouping neighboring elements with xsl

Discussion in 'XML' started by Dimitre Novatchev, Nov 28, 2003.

  1. "kcwolle" <> wrote in message news:...
    > Hello,
    > I have an XSL stylesheet problem.
    > I want to group neighboring elements by a surrounding hierarchical
    > element, for example all neighboring div Elements with attribute class
    > equals 1
    > Source:
    > <p>text</p>
    > <p>text</p>
    > <div class="1">text1</div>
    > <div class="1">text2</div>
    > <p>text</p>
    > <p>text</p>
    > <div class="1">text1</div>
    > <div class="1">text2</div>
    > <div class="1">text3</div>
    > <p>text</p>
    >
    > Desired Output:
    > <p>text</p>
    > <p>text</p>
    > <div class="1">
    > <p>text1</p>
    > <p>text2</p>
    > </div>
    > <p>text</p>
    > <p>text</p>
    > <div class="1">
    > <p>text1</p>
    > <p>text2</p>
    > <p>text3</p>
    > </div>
    > <p>text</p>
    >
    > Can anybody help me to solve this problem?



    Yes,

    This transformation:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:key name="kNeighbors"
    match="div[@class=1]"
    use="generate-id(preceding-sibling::*
    [not(self::div[@class=1])][1]
    )"/>

    <xsl:eek:utput omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node() | @*">
    <xsl:copy>
    <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="div[@class=1]"/>

    <xsl:template
    match="div[@class=1
    and
    not(preceding-sibling::*[1]
    [self::div[@class=1]]
    )
    ]">
    <div class="1">
    <xsl:apply-templates mode="Copy"
    select="key('kNeighbors',
    generate-id(preceding-sibling::*
    [not(self::div[@class=1])]
    [1]
    )
    )"/>
    </div>
    </xsl:template>

    <xsl:template match="div" mode="Copy">
    <p><xsl:copy-of select="node()"/></p>
    </xsl:template>

    </xsl:stylesheet>

    when applied on your source.xml (imbedded in a top html element to
    make it well-formed):

    <html>
    <p>text</p>
    <p>text</p>
    <div class="1">text1</div>
    <div class="1">text2</div>
    <p>text</p>
    <p>text</p>
    <div class="1">text1</div>
    <div class="1">text2</div>
    <div class="1">text3</div>
    <p>text</p>
    </html>

    produces the wanted result:

    <html>
    <p>text</p>
    <p>text</p>
    <div class="1">
    <p>text1</p>
    <p>text2</p>
    </div>
    <p>text</p>
    <p>text</p>
    <div class="1">
    <p>text1</p>
    <p>text2</p>
    <p>text3</p>
    </div>
    <p>text</p>
    </html>


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL
     
    Dimitre Novatchev, Nov 28, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank-Ralph Reiser

    Re: XSL: grouping data and group header?

    Frank-Ralph Reiser, Jul 31, 2003, in forum: XML
    Replies:
    0
    Views:
    1,448
    Frank-Ralph Reiser
    Jul 31, 2003
  2. kcwolle
    Replies:
    0
    Views:
    383
    kcwolle
    Nov 27, 2003
  3. Jack Wayne
    Replies:
    0
    Views:
    469
    Jack Wayne
    Jan 24, 2005
  4. Replies:
    1
    Views:
    3,639
    A. Bolmarcich
    May 27, 2005
  5. Rob Smegma
    Replies:
    1
    Views:
    1,829
    shaun
    Sep 26, 2005
Loading...

Share This Page