[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Merging adjacent elements

Subject: Merging adjacent elements
From: "Rick Quatro rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 28 Aug 2015 21:25:10 -0000
 Merging adjacent elements
I have this xml content:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <p>This is a <code>test</code> of <code><i>adjacent</i>
</code><code>code </code><code>elements</code> to see if we can merge
them.</p>
</root>

What I want to end up with is this:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <p>This is a <code>test</code> of <code><i>adjacent</i> code
elements</code> to see if we can merge them.</p>
</root>

Here is my XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:output indent="yes"/>
    
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="p">
        <xsl:copy>
            <xsl:for-each-group select="*"
group-adjacent="boolean(self::code)">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <code>
                            <xsl:apply-templates
select="current-group()/node()"/>
                        </code> 
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>  
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Here is my output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <p>
       <code>test<i>adjacent</i> code elements</code>
   </p>
</root>

How do I get the rest of the <p> content from the input? Thanks in advance.

Rick

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.