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 6798075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:41:10+00:00 2026-05-26T18:41:10+00:00

I have some XML where I would like to remove identical consecutive child nodes,

  • 0

I have some XML where I would like to remove identical consecutive child nodes, which are in different parents. That is, if a child (in different parents) node my XML tree appears two times or more consecutively, I want to remove all the duplicates.

The duplicate nodes I’m thinking of are the <child>a</child> in the first two <parent> nodes.

An example:

Here is the source XML:

<root>
   <parent>
      <child>a</child>
      <child>b</child>
      <child>c</child>
   </parent>

   <parent>
      <child>a</child>
      <child>bb</child>
      <child>cc</child>
   </parent>

   <parent>
      <child>aaa</child>
      <child>bbb</child>
      <child>ccc</child>
   </parent>

   <parent>
      <child>a</child>
      <child>bbbb</child>
      <child>cccc</child>
   </parent>

</root>

Here is the desired XML:

<root>
   <parent>
      <child>a</child>
      <child>b</child>
      <child>c</child>
   </parent>

   <parent>
      <child>bb</child>
      <child>cc</child>
   </parent>

   <parent>
      <child>aaa</child>
      <child>bbb</child>
      <child>ccc</child>
   </parent>

   <parent>
      <child>a</child>
      <child>bbbb</child>
      <child>cccc</child>
   </parent>

</root>

Only one element is removed but if there were, for example, 5 consecutive <child>a</child> nodes at the beginning (instead of 2), four of them would be removed. I’m using XSLT 2.0.

I appreciate any help.

Follow-Up:

Thanks to Kirill I get the documents I want, however this has spawned a new problem that I didn’t anticipate, if I have an XML document like this:

<root>
   <parent>
      <child>a</child>
      <child>b</child>
      <child>c</child>
   </parent>

   <parent>
      <child>a</child>
      <child>b</child>
      <child>c</child>
   </parent>

   <parent>
      <child>aaa</child>
      <child>bbb</child>
      <child>ccc</child>
   </parent>

</root>

And I apply Kirill’s XSLT, I get this:

<root>
   <parent>
      <child>a</child>
      <child>b</child>
      <child>c</child>
   </parent>

   <parent>
   </parent>

   <parent>
      <child>aaa</child>
      <child>bbb</child>
      <child>ccc</child>
   </parent>

</root>

How can I also remove the <parent> </parent>? For my application there may be other subelements of <parent>, which are OK to remove if there is no <child> element in the <parent> element.

A solution I have, that I don’t like, is to apply another transform after the first one. This only works when applied in order though and I need a separate XSLT file and need to run two commands instead of one.

Here it is:

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

 <xsl:template match="parent[not(child)]"/>
  • 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-26T18:41:11+00:00Added an answer on May 26, 2026 at 6:41 pm

    Use:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    
      <xsl:template match="child[../preceding-sibling::parent[1]/child = .]"/>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that I would like to map some attributes of
i have some xml with math ml inside, and i would like to find
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I have some XML which contains records and sub records, like this: <data> <record
If I have some xml like this: <mynode> <mysubnode> <mysubsubnode>hello world</mysubsubnode> some more text
I have some XML which I'm trying to search: <debts> <section id=24 description=YYYYY> <section
I have some xml like this: <Data> <Rows> <Row> <Field Name=title>Mr</Field> <Field Name=surname>Doe</Field> <Row>
I would like to remove a tag from some HTML without stripping the remaining
So I have 2 models. I have a medicalBillModel that I would like to
I have some code that looks like this (I left out the parts that

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.