Leonid Bor
Leonid Bor

Reputation: 2264

XSLT: Merge values of siblings with same attribute value

Given the input like:

<root>
    <childA style="style_1">Some</childA>
    <childA style="style_1">Text</childA>
    <childA style="style_1">Here</childA>
    <childB style="style_2"/>
    <childA style="style_2">Fake</childA>
    <childB style="style_2"/>
    <childA style="style_1">Some</childA>
    <childA style="style_1">Other</childA>
    <childB style="style_2"/>
    <childA style="style_1">Text</childA>
    <parent>            
        <childA style="style_1">More</childA>
        <childA style="style_1">Text</childA>
    </parent>
</root>

How do I merge elements that are following each other? So the desired output is:

<root>
    <childA style="style_1">SomeTextHere</childA>
    <childB style="style_2"/>
    <childA style="style_2">Fake</childA>
    <childB style="style_2"/>
    <childA style="style_1">SomeOther</childA>
    <childB style="style_2"/>
    <childA style="style_1">Text</childA>
    <parent>            
        <childA style="style_1">MoreText</childA>
    </parent>
</root>

I tried different tricks with for-each-group and for-each inside of it, but got duplicated nodes and also that <childB> between two <childA> nodes was ignored when I used group-adjacent="@style" and I got SomeOtherText in one node instead of two nodes.

Here's my attempt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output indent="yes" />
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="childA">
        <xsl:copy-of select="." />
    </xsl:template>

    <xsl:template match="childB">
        <xsl:copy-of select="." />
    </xsl:template>

    <xsl:template match="parent">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[childA]">
        <xsl:copy>
            <xsl:for-each-group select="childA" group-adjacent="@style">
                <xsl:element name="childA">
                    <xsl:attribute name="style">
                        <xsl:value-of select="current-grouping-key()"/>
                    </xsl:attribute>
                    <xsl:for-each select="current-group()">
                        <xsl:value-of select="."/>
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each-group>
        </xsl:copy>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="text()"/>

</xsl:stylesheet>

Upvotes: 0

Views: 729

Answers (3)

John Bollinger
John Bollinger

Reputation: 180103

You can do this (as clarified in comments) without relying on XSLT 2.0 or 3.0 features, though perhaps at a performance penalty vs. the best available with a later version. You simply need to know how to write an XPath expression that distinguishes the first <childA> in each group from the rest, or vise versa. For example:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="childA">
    <!-- this is the first childA in a group -->
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="." mode = "merge"/>
    </xsl:copy>
  </xsl:template>

  <!-- Matches (only) the second and subsequent members of a childA group: -->
  <xsl:template match="childA[name(preceding-sibling::*[1]) = 'childA' and @style = preceding-sibling::*[1]/@style]"/>

  <!-- merge in this childA's content and that of the remaining group elements -->
  <xsl:template match="childA" mode="merge">
    <xsl:apply-templates select="node()"/> <!-- attributes ignored -->
    <!-- merge the next sibling, too, if it's in the same group: -->
    <xsl:apply-templates select="following-sibling::*[1][name() = 'childA' and @style = preceding-sibling::*[1]/@style]" mode = "merge"/>
  </xsl:template>

</xsl:stylesheet>

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 116957

How about:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*[childA]">
    <xsl:copy>
        <xsl:for-each-group select="*" group-adjacent="concat(name(), '|', @style)">
            <xsl:choose>
                <xsl:when test="self::childA">
                    <childA style="{@style}">
                        <xsl:value-of select="current-group()" separator=""/>
                    </childA>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 3

Martin Honnen
Martin Honnen

Reputation: 167471

Using XSLT 3.0 (like supported by Saxon 9.8 or actual Altova) I think you want:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:output indent="yes"/>

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each-group select="*"
                group-adjacent="node-name() eq xs:QName('childA'), @style" composite="yes">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()[1]">
                        <xsl:copy>
                            <xsl:apply-templates select="@*, current-group()/node()"/>
                        </xsl:copy>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 0

Related Questions