title: Filter Attributes Using XSLT based on Attribute Names
body: <p>Sorry if this has been discussed before, but could nt find a suitable answer for my problem.....
  I have this xml:</p>
  
  <pre><code>&lt;RootList&gt;
  &lt;Root RootAtt1="1" RootAtt2="2" RootAtt3="3"&gt;
  &lt;Extn ExtnAtt1="1"&gt;
  &lt;Child ChildAtt1="1" ChildAtt2="2"/&gt;
  &lt;/Extn&gt;
  &lt;/Root&gt;
  &lt;/RootList&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>So if the input comes like: </p>
  
  <pre><code>&lt;RootList&gt;
  &lt;Root RootAtt1="1" RootAtt2="2" RootAtt3="3"&gt;
  &lt;Extn ExtnAtt1="1" ExtnAtt2="2"&gt;
  &lt;Child ChildAtt1="1" ChildAtt2="2"/&gt;
  &lt;/Extn&gt;
  &lt;/Root&gt;
  &lt;/RootList&gt;
  </code></pre>
  
  <p>then the output will be like:</p>
  
  <pre><code>&lt;RootList&gt;
  &lt;Root RootAtt1="1" RootAtt2="2" RootAtt3="3"&gt;
  &lt;Extn ExtnAtt1="1"&gt;
  &lt;Child ChildAtt1="1" ChildAtt2="2"/&gt;
  &lt;/Extn&gt;
  &lt;/Root&gt;
  &lt;/RootList&gt;
  </code></pre>
  
  <p>For this I have written an xsl as: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
     &lt;xsl:output method="xml" indent="yes"/&gt; 
  &lt;xsl:template match="/"&gt;
  &lt;xsl:apply-templates/&gt;
  
  &lt;RootList&gt;
  &lt;xsl:for-each select="/RootList"&gt;
  &lt;xsl:element name="Root"&gt;
  &lt;xsl:element name="Extn"&gt;
  &lt;xsl:attribute name="ExtnAtt1"&gt;
  &lt;xsl:value-of select="/RootList/Root/Extn/@ExtnAtt1"/&gt;
  &lt;/xsl:attribute&gt;
  &lt;/xsl:element&gt;
  &lt;/xsl:element&gt;
  &lt;/xsl:for-each&gt;
  &lt;/RootList&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>But this gives me the output as:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-16"?&gt;
  &lt;RootList&gt;
  &lt;Root&gt;
  &lt;Extn ExtnAtt1="1" /&gt;
  &lt;/Root&gt;
  &lt;/RootList&gt;
  </code></pre>
  
  <p>I want to preserve the existing xml and only want remove attributes that are extra.</p>
  
  <p>Can anybody please help me with this.</p>
  
  <p>Thanks in advance guys.</p>
  
  <p>Thanks guys for all the replies...you guys rock....
  I figured another method....let me know what do u think about this....</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
     &lt;xsl:output method="xml" indent="yes"/&gt; 
  &lt;xsl:template match="/"&gt;
  &lt;xsl:apply-templates/&gt;
  &lt;RootList&gt;
  &lt;xsl:for-each select="/RootList"&gt;
  &lt;xsl:element name="Root"&gt;
  &lt;xsl:copy-of select="/RootList/Root/@RootAtt1"/&gt;
  &lt;xsl:copy-of select="/RootList/Root/@RootAtt2"/&gt;
  &lt;xsl:copy-of select="/RootList/Root/@RootAtt3"/&gt;
  &lt;xsl:element name="Extn"&gt;
  &lt;xsl:copy-of select="/RootList/Root/Extn/@ExtnAtt1"/&gt;
  &lt;xsl:element name="Child"&gt;
  &lt;xsl:copy-of select="/RootList/Root/Extn/Child/@ChildAtt1"/&gt;
  &lt;xsl:copy-of select="/RootList/Root/Extn/Child/@ChildAtt2"/&gt;
  &lt;/xsl:element&gt;
  &lt;/xsl:element&gt;
  &lt;/xsl:element&gt;
  &lt;/xsl:for-each&gt;
  &lt;/RootList&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>When this is applied on :</p>
  
  <pre><code>&lt;RootList&gt;
  &lt;Root RootAtt1="1" RootAtt2="2" RootAtt3="3"&gt;
  &lt;Extn ExtnAtt1="1" ExtnAtt2="2"&gt;
  &lt;Child ChildAtt1="1" ChildAtt2="2"/&gt;
  &lt;/Extn&gt;
  &lt;/Root&gt;
  &lt;/RootList&gt;
  </code></pre>
  
  <p>It gives an output as:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-16"?&gt;
  &lt;RootList&gt;
  &lt;Root RootAtt1="1" RootAtt2="2" RootAtt3="3"&gt;
  &lt;Extn ExtnAtt1="1"&gt;
  &lt;Child ChildAtt1="1" ChildAtt2="2" /&gt;
  &lt;/Extn&gt;
  &lt;/Root&gt;
  &lt;/RootList&gt;
  </code></pre>
  
  <p>assuming that RootList\Root\Extn\@ExtnAtt2 is the unwanted attribute.</p>
  
  <p>Thanks
  Awaiting your reply....:)</p>
  
  <p>Cheers</p>