"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"
xmlns

sl="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

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