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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:26:33+00:00 2026-05-27T19:26:33+00:00

Sorry if this has been discussed before, but could nt find a suitable answer

  • 0

Sorry if this has been discussed before, but could nt find a suitable answer for my problem…..
I have this xml:

<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1">
<Child ChildAtt1="1" ChildAtt2="2"/>
</Extn>
</Root>
</RootList>

What I want is if any extra attribute comes with this xml at RootList\Root\Extn node apart from what is present, that attribute will be removed.

So if the input comes like:

<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1" ExtnAtt2="2">
<Child ChildAtt1="1" ChildAtt2="2"/>
</Extn>
</Root>
</RootList>

then the output will be like:

<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1">
<Child ChildAtt1="1" ChildAtt2="2"/>
</Extn>
</Root>
</RootList>

For this I have written an xsl as:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes"/> 
<xsl:template match="/">
<xsl:apply-templates/>

<RootList>
<xsl:for-each select="/RootList">
<xsl:element name="Root">
<xsl:element name="Extn">
<xsl:attribute name="ExtnAtt1">
<xsl:value-of select="/RootList/Root/Extn/@ExtnAtt1"/>
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:for-each>
</RootList>
</xsl:template>
</xsl:stylesheet>

But this gives me the output as:

<?xml version="1.0" encoding="UTF-16"?>
<RootList>
<Root>
<Extn ExtnAtt1="1" />
</Root>
</RootList>

I want to preserve the existing xml and only want remove attributes that are extra.

Can anybody please help me with this.

Thanks in advance guys.

Thanks guys for all the replies…you guys rock….
I figured another method….let me know what do u think about this….

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes"/> 
<xsl:template match="/">
<xsl:apply-templates/>
<RootList>
<xsl:for-each select="/RootList">
<xsl:element name="Root">
<xsl:copy-of select="/RootList/Root/@RootAtt1"/>
<xsl:copy-of select="/RootList/Root/@RootAtt2"/>
<xsl:copy-of select="/RootList/Root/@RootAtt3"/>
<xsl:element name="Extn">
<xsl:copy-of select="/RootList/Root/Extn/@ExtnAtt1"/>
<xsl:element name="Child">
<xsl:copy-of select="/RootList/Root/Extn/Child/@ChildAtt1"/>
<xsl:copy-of select="/RootList/Root/Extn/Child/@ChildAtt2"/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:for-each>
</RootList>
</xsl:template>
</xsl:stylesheet>

When this is applied on :

<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1" ExtnAtt2="2">
<Child ChildAtt1="1" ChildAtt2="2"/>
</Extn>
</Root>
</RootList>

It gives an output as:

<?xml version="1.0" encoding="UTF-16"?>
<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1">
<Child ChildAtt1="1" ChildAtt2="2" />
</Extn>
</Root>
</RootList>

assuming that RootList\Root\Extn\@ExtnAtt2 is the unwanted attribute.

Thanks
Awaiting your reply….:)

Cheers

  • 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-27T19:26:34+00:00Added an answer on May 27, 2026 at 7:26 pm

    One way to do this is to override the identity template, and add a template to match the attributes you wish to remove, which will just ignore them.

    <xsl:template match="Extn/@*[not(local-name() = 'ExtnAtt1')]" />
    

    So, given the following XSLT:

    <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="Extn/@*[not(local-name() = 'ExtnAtt1')]"/>
    </xsl:stylesheet>
    

    When applied to the following XML

    <RootList>   
      <Root RootAtt1="1" RootAtt2="2" RootAtt3="3">   
        <Extn ExtnAtt1="1" ExtnAtt2="2">   
          <Child ChildAtt1="1" ChildAtt2="2"/>   
        </Extn>   
      </Root>   
    </RootList> 
    

    The following will be output:

    <RootList>
      <Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
        <Extn ExtnAtt1="1">
          <Child ChildAtt1="1" ChildAtt2="2"></Child>
        </Extn>
      </Root>
    </RootList>
    

    An alternate method, if you require to keep multiple attributes on the Extn element, is to use a stylesheet like so, which explictly lists the attributes to keep:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:template match="Extn">
          <xsl:copy>
             <xsl:apply-templates select="@ExtnAtt1|@ExtnAtt3|@ExtnAtt5|node()"/>
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="@*|node()">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to the following XML

    <RootList>
      <Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
        <Extn ExtnAtt1="1" ExtnAtt2="2" ExtnAtt3="3" ExtnAtt4="4" ExtnAtt5="5">
          <Child ChildAtt1="1" ChildAtt2="2"/>
        </Extn>
      </Root>
    </RootList>
    

    The following is output

    <RootList>
      <Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
        <Extn ExtnAtt1="1" ExtnAtt3="3" ExtnAtt5="5">
          <Child ChildAtt1="1" ChildAtt2="2"></Child>
        </Extn>
       </Root>
    </RootList>
    

    Note that all other elements, apart from the Extn element, are left unaffected.

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

Sidebar

Related Questions

So sorry if this has been answered before, but I cannot find an answer
Sorry if this has been asked before, I did check but couldn't find anything...
Sorry if this has been asked before but I've tried searching and can't find
Sorry if this has been asked before, but I couldn't find a solution to
Sorry if this has been discussed somewhere else on stackoverflow (I could not find
Sorry if this has been asked before, I searched but couldn't find anything. I'm
Sorry if this has been asked before, but I can't find a clear enough
Sorry if this has been asked before. I couldn't find a definitive answer. Can
Sorry if this has been asked elsewhere. I have looked but can't find any
Sorry if this has been asked before, but I was unable to find it.

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.