Altova Mailing List Archives

Re: [xsl] Merging adjacent nodes of same type

From: David Carlisle <davidc@--------->
Date: 9/23/2005 11:32:00 AM
> 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:


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.