Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > .Re: Grouping neighboring elements with xsl

Reply
Thread Tools

.Re: Grouping neighboring elements with xsl

 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      11-28-2003
"kcwolle" <> wrote in message news: om...
> 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"
xmlnssl="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]
)"/>

<xslutput 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl to group elements? [xsl newbie] Rob Smegma XML 1 09-26-2005 10:59 AM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
Sorting and grouping with XSL Stylesheets Jack Wayne XML 0 01-24-2005 07:32 PM
Grouping neighboring elements with xsl kcwolle XML 0 11-27-2003 09:34 PM
Re: XSL: grouping data and group header? Frank-Ralph Reiser XML 0 07-31-2003 07:53 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57