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

  • Home
  • SEARCH
  • 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 7829813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:55:23+00:00 2026-06-02T10:55:23+00:00

I need to remove some nodes in my xml file in which it contains

  • 0

I need to remove some nodes in my xml file in which it contains certain method and by what order it appears for example here is the input:

<root> 
<node id="a">
    <section id="a_1">
        <item id="0">
            <attribute>
                <color>Red</color>
            </attribute>
        </item>
    </section>

    <section id="a_2">
        <item id="0">
            <attribute>
                <color>Red</color>
            </attribute>
        </item>
    </section>            
</node>

<node id="b">
    <section id="b_1">         
        <user id="b_1b" method="pause">
            <attribute>a</attribute>
        </user>      
        <user id="b_1b" method="run">
            <attribute>a</attribute>
        </user>             
        <user id="b_1a" method="run">
            <attribute>
                <name>John</name>
            </attribute>
        </user>

        <user id="b_1a" method="pause">
            <attribute>a</attribute>
        </user>
    </section>

    <section id="b_1" method="create">   

        <user id="b_1b" method="stop">
            <attribute>a</attribute>
        </user>            
        <user id="b_1a" method="stop">
            <attribute>a</attribute>
        </user>

        <user id="b_1b" method="run">
            <attribute>a</attribute>
        </user>
        <user id="b_1b" method="pause">
            <attribute>a</attribute>
        </user>
    </section>

    <section id="b_2">                
        <user id="b_1a" method="run">
            <attribute>
                <name>John</name>
            </attribute>
        </user>
    </section>
</node>

If I use this method:

<xsl:transform version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:my="http://localhost"
  exclude-result-prefixes="my">

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

  <xsl:template match=" user[not(@method eq 'stop' )][not(. is my:last(.))]
  | user[    @method eq 'stop' ][not(. is my:last(.))]
                     | user[not(@method eq 'stop')][my:last(.)/@method eq 'stop']"/>

          <!-- Get the last user for this section & user id -->
          <xsl:function name="my:last">
            <xsl:param name="user"/>
            <xsl:sequence select="($user/../../section[@id eq $user/../@id]
                                              /user   [@id eq $user/@id]
                                  )
                                  [last()]"/>
          </xsl:function>
</xsl:transform>

result will be:

<root>
    <node id="a">
        <section id="a_1">
            <item id="0">
                <attribute>
                    <color>
Red
                    </color>
                </attribute>
            </item>
        </section>
        <section id="a_2">
            <item id="0">
                <attribute>
                    <color>
Red
                    </color>
                </attribute>
            </item>
        </section>
    </node>
    <node id="b">
        <section id="b_1">
        </section>
        <section id="b_1" method="create">
            <user id="b_1a" method="stop">
                <attribute>
a
                </attribute>
            </user>
            <user id="b_1b" method="pause">
                <attribute>
a
                </attribute>
            </user>
        </section>
        <section id="b_2">
            <user id="b_1a" method="run">
                <attribute>
                    <name>
John
                    </name>
                </attribute>
            </user>
        </section>
    </node>
</root>

while the expected output is:

<root>
    <node id="a">
        <section id="a_1">
            <item id="0">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </section>

        <section id="a_2">
            <item id="0">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </section>            
    </node>

    <node id="b">
        <section id="b_1">                                           
        </section>

        <section id="b_1" method="create">               
            <user id="b_1a" method="stop">
                <attribute>a</attribute>
            </user>

            **<user id="b_1b" method="run">
                <attribute>a</attribute>
            </user>**
            <user id="b_1b" method="pause">
                <attribute>a</attribute>
            </user>
        </section>

        <section id="b_2">                
            <user id="b_1a" method="run">
                <attribute>
                    <name>John</name>
                </attribute>
            </user>
        </section>
    </node>
</root>

so this is how the order works. If ‘stop’ occurs last then any other node with other methods with the same user id (such as pause and run) will be removed.
But if it’s not then the node with ‘stop’ itself and All node before that ‘stop’ will be removed.

This has to be occurred in the same section id and it will delete only the user node (leave the section id even if it is empty after deletion of user node).
Hope the explanation it’s not confusing.

Thanks very much.

cheers,
John

  • 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-06-02T10:55:26+00:00Added an answer on June 2, 2026 at 10:55 am

    Try this XSLT instead, which reduces the number of conditions, and will work in XSLT 1.0 too

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
    
       <!-- Ignore users with 'stop' method which are not the last such user -->
       <xsl:template match="user[@method='stop'][following::user[@method='stop']]"/>
    
       <!-- Match other users -->
       <xsl:template match="user">
          <!-- Copy the user if there isn't a following user with the same id and 'stop' method -->
          <xsl:if test="not(following::user[@id=current()/@id][@method='stop'])">
             <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
             </xsl:copy>
          </xsl:if>
       </xsl:template>
    
       <xsl:template match="@*|node()">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>
    

    This should also generate the expected output

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to remove some nodes in my xml file in which it contains
I need to remove elements in some xml files. How do I correctly ensure
I've got a fairly complex XML document, where I need to remove specific nodes
I need to rip some xml nodes from a xml document. The source is
Here I need to cache some entites, for example, a Page Tree in a
I need to remove a big amount of virtual directories, some of them don't
I've got a Perl script which consumes an XML file on Linux and occasionally
I need to parse the following xml document (which is coming from external web
I looked at How to remove duplicate XML nodes using XSLT and other related
I need some help editing a csproj file using PowerShell. I basically need to

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.