Re: [xsl] Merging adjacent nodes of same type

Subject: Re: [xsl] Merging adjacent nodes of same type
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 23 Sep 2005 12:31:39 +0100
> Is it possible without this?

oh everything's possible, just different.

This is a fairly standard grpuping question (google your way to jeni's

You have to be a bit careful about what exactly you mean by adjacent
your <text> element has mixed content: element nodes and text nodes as
children so normally you wouldn't ignore text nodes in that case
which would mean that your offset elements were not adjacent (being
separated by some whitespace). 
so assuming your real example does have "Some text" and not
<foo>Some text</foo> you can't test for preceding-sibling::*[1] as that
would not see the text nodes, and you can't test for
preceding-sibling::node()[1] as that would see the white space text,
so by adjacent you mean
preceding-sibling::node()[self::* or normalize-space(.)][1] 
that is the nearest sibling that is an element or has non whitespace
characters in its string value.

so something like:

<xsl:stylesheet xmlns:xsl=""; version="1.0" 

<xsl:template match="node()">
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()[1]"/>
<xsl:apply-templates select="following-sibling::node()[1]"/>

<xsl:template match="offset[not(node())]">
<xsl:param name="a" select="/.."/>
<xsl:when test="
   following-sibling::node()[self::* or normalize-space(.)][1]/self::offset[not(node())]">
 <xsl:apply-templates select="following-sibling::offset[1]">
  <xsl:with-param name="a" select="$a|@*"/>
<offset  x="{sum($a[name()='x']|@x)}"  y="{sum($a[name()='y']|@y)}" z="{sum($a[name()='z']|@z)}"/>
<xsl:apply-templates select="following-sibling::node()[1]"/>


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:

Current Thread