[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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
site)

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="http://www.w3.org/1999/XSL/Transform" version="1.0" 
>


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

<xsl:template match="offset[not(node())]">
<xsl:param name="a" select="/.."/>
<xsl:choose>
<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|@*"/>
 </xsl:apply-templates>
</xsl:when>
 <xsl:otherwise>
<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]"/>
 </xsl:otherwise>
</xsl:choose>
</xsl:template>




</xsl:stylesheet>

________________________________________________________________________
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:
http://www.star.net.uk
________________________________________________________________________


Current Thread