Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 471527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:59:41+00:00 2026-05-12T23:59:41+00:00

Is it possible to merge every sequence of nodes of the same specified type?

  • 0

Is it possible to merge every sequence of nodes of the same specified type? (‘aaa’ in this case) (not just first occurrence of sequence)

Here is my XML input:

<block>
    <aaa>text1</aaa>
    <aaa>text2</aaa>
    <aaa><xxx>text3</xxx></aaa>
    <bbb>text4</bbb>
    <aaa>text5</aaa>
    <bbb><yyy>text6</yyy></bbb>
    <bbb>text7</bbb>
    <aaa>text8</aaa>
    <aaa><zzz>text9</zzz></aaa>
    <aaa>texta</aaa>
</block>

And I want following output:

<block>
    <aaa>text1text2<xxx>text3</xxx></aaa>
    <bbb>text4</bbb>
    <aaa>text5</aaa>
    <bbb><yyy>text6</yyy></bbb>
    <bbb>text7</bbb>
    <aaa>text8<zzz>text9</zzz>texta</aaa>
</block>

Any help appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:59:42+00:00Added an answer on May 12, 2026 at 11:59 pm

    Here is another way to do this.

    First, match on all the child nodes of the block element

    <xsl:template match="block/child::*">
    

    Next, check if the element’s most direct sibling has a different name, indicating this is the first of one or more adjacent elements:

    <xsl:if test="local-name(preceding-sibling::*[position()=1]) != $name">
    

    If so, you can copy that node. Then, you need to copy of following siblings with the same name. I did this by recursively calling a template on each immediately following sibling with the same name

    <xsl:apply-templates select="following-sibling::*[1][local-name()=$name]" mode="next"/>
    

    Putting this all together gives

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
    
       <!-- Match children of the block element -->
       <xsl:template match="block/child::*">
          <xsl:variable name="name" select="local-name()"/>
    
          <!-- Is this the first element in a sequence? -->
          <xsl:if test="local-name(preceding-sibling::*[position()=1]) != $name">
             <xsl:copy>
                <xsl:apply-templates />
    
                <!-- Match the next sibling if it has the same name -->
                <xsl:apply-templates select="following-sibling::*[1][local-name()=$name]" mode="next"/>
             </xsl:copy>
          </xsl:if>
       </xsl:template>
    
       <!-- Recursive template used to match the next sibling if it has the same name -->
       <xsl:template match="block/child::*" mode="next">
          <xsl:variable name="name" select="local-name()"/>
             <xsl:apply-templates />
          <xsl:apply-templates select="following-sibling::*[1][local-name()=$name]" mode="next"/>
       </xsl:template>
    
       <!-- Template used to copy a generic node -->
       <xsl:template match="@* | node()">
             <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
             </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to merge Google Fusion Tables using GQL or other Javascript/REST API?
Is it possible to merge a range of revisions from one branch to another
Is it possible to merge changes from a central repo to a local branch
Is it possible to merge two JSON documents with the Jackson JSON library? I
Is that possible to distinguish if merge was performed manually or automatically? I'm using
How could I merge these into one line? I'm sure it's possible. RewriteRule ^forums$
Is it possible to create merged cells in SlickGrid? If not, then what other
Is it possible to use a custom HTTP VERB, like MERGE, with jQuery.ajax()? I'm
Sorry if answers to this question already exist, I did not find them yet.
We have strictly defined which relationships are CascadeType.MERGE in our app. This plays into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.