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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:00:20+00:00 2026-06-04T20:00:20+00:00

Input file: <root> <sector> <nodeA id=a> <section id=i> <item1 id=1 method=create> <somechild>a</somechild> </item1> <item1

  • 0

Input file:

<root>
    <sector>
        <nodeA id="a">
            <section id="i">
                <item1 id="1" method="create"> 
                    <somechild>a</somechild>
                </item1>

                <item1 id="1" method="delete" />
            </section>                   
        </nodeA>

        <nodeA id="b">
            <cell id="ii">  
                <item2 id="2" method="create"> 
                    <otherchild>a</otherchild>
                </item2>
            </cell>
            <cell id="ii">
                <item2 id="2" method="delete" />
            </cell>                   
        </nodeA>

        <nodeB id="i">
            <cell id="ii">  
                <item3 id="1" method="create"> 
                    <child>b</child>
                </item3>
            </cell>

            <cell id="ii">
                <item3 id="1" method="delete" />
                <item3 id="1" method="create"> 
                    <otherchild>a</otherchild>
                </item3>
            </cell>                   
        </nodeB>
    </sector> 
</root>

Expected Output:

<root>
    <sector>
        <nodeA id="a">
            <section id="i">
            </section>                   
        </nodeA>

        <nodeA id="b">
            <cell id="ii">  
            </cell>
            <cell id="ii">
            </cell>                   
        </nodeA>

        <nodeB id="i">
            <cell id="ii">  
                <item3 id="1" method="create"> <!-- this is not eliminated as it violates the rule by having create followed by delete and followed by create again -->
                    <child>b</child>
                </item3>
            </cell>

            <cell id="ii">
                <item3 id="1" method="delete" />
                <item3 id="1" method="create"> 
                    <otherchild>a</otherchild>
                </item3>
            </cell>                   
        </nodeB>
    </sector> 
</root>

I need to eliminate node in xml using this rule:

  • one node with method="create" followed with one node with method="delete" will be eliminated
  • the scenario can happen in one parent or spread in two parent as long as it has the same element name and id <cell id="ii">

How can I do this transformation using XSLT 1.0 or 2.0?

Thank you.

  • 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-04T20:00:22+00:00Added an answer on June 4, 2026 at 8:00 pm

    Here is one approach. You define a key to group your items you are looking to remove. I think you are grouping by the @id attribute of the element, together with the @id attribute of the two parent nodes

    <xsl:key 
       name="items" 
       match="*[@method != '']" use="concat(@id, '|', ../@id, '|', ../../@id)" />
    

    Next, you could have a template to match your @method=’create’ items where there are two elements in the key, and the other item is a @method=’delete’

    <xsl:template match="*
       [@method = 'create']
       [count(key('items', concat(@id, '|', ../@id, '|', ../../@id))) = 2]
       [key('items', concat(@id, '|', ../@id, '|', ../../@id))[@method = 'delete']]" />
    

    You would also need a template to match the other @method=’delete’ in a similar manner.

    Here is the full XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:key name="items" match="*[@method != '']" use="concat(@id, '|', ../@id, '|', ../../@id)" />
    
       <xsl:template match="*[@method = 'create'][count(key('items', concat(@id, '|', ../@id, '|', ../../@id))) = 2][key('items', concat(@id, '|', ../@id, '|', ../../@id))[@method = 'delete']]" />
       <xsl:template match="*[@method = 'delete'][count(key('items', concat(@id, '|', ../@id, '|', ../../@id))) = 2][key('items', concat(@id, '|', ../@id, '|', ../../@id))[@method = 'create']]" />
    
       <xsl:template match="@*|node()" name="identity">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output

    <root>
       <sector>
          <nodeA id="a">
             <section id="i"/>
          </nodeA>
          <nodeA id="b">
             <cell id="ii"/>
             <cell id="ii"/>
          </nodeA>
          <nodeB id="i">
             <cell id="ii">
                <item3 id="1" method="create">
                   <child>b</child>
                </item3>
             </cell>
             <cell id="ii">
                <item3 id="1" method="delete"/>
                <item3 id="1" method="create">
                   <otherchild>a</otherchild>
                </item3>
             </cell>
          </nodeB>
       </sector>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Input file: <myroot> <nodeA id=a> <section id=i> <item id=0 method=a> <!-- parent section id=i
this is an example of my input file. <root> <!-- [...] --> <bbb>Foo 1</bbb>
My task is to create a parent-child hierarchy file using perl. Sample Input file
Given an input xml file with following structure: <root> <record row=1 col=1 val=1 />
I'm trying to create a singly linked list from an input text file for
This is my input file .. [root@localhost scripts]# cat ip6hdr.txt | xargs -n4 6000
I have this problem for xslt: This is the input file: <root> <header/> <item/>
If I have this input file in xml: <root> <node id=N1> <fruit id=1> <orange
Sample Input File +--------------------+---------+--------- | Name | S1 | S2 +--------------------+---------+--------- | A |
My input file(f) has some Unicode (Swedish) that isn't being read correctly. Neither of

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.