title: XSLT issue with renaming the elements -- changing namespace
body: <p>I am trying to rename/remove the namespace prefix from the output XML.
  However, one of the top element should contain the namespace declaration without prefix.</p>
  
  <p>The XSLT is not working properly. In some instances I can still see the namespace prefix in the output XML and the namespace doesn't show up in the element I want to see.</p>
  
  <p>I've tried to use the exclude-result-prefixes and some templates but not working properly.
  Just to clarify I'm using XSLT 1.0</p>
  
  <p>Thanks in Advance ...</p>
  
  <p><strong>XSLT:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
      xmlns:ns0="http://somenamespace"
      xmlns:ac="http://ac.namespace"
      exclude-result-prefixes="ns0 msxsl ac"&gt;
  
    &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes" encoding="utf-8"/&gt;
  
    &lt;xsl:template match="node() | @*"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates select="node() | @*" /&gt;
      &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
  
  
    &lt;xsl:template match="node()|comment()|processing-instruction()"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates/&gt;
      &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="*"&gt;
      &lt;xsl:element name="{local-name()}"&gt;
        &lt;xsl:apply-templates select="@*|node()"/&gt;
      &lt;/xsl:element&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="@*"&gt;
      &lt;xsl:attribute name="{local-name()}"&gt;
        &lt;xsl:value-of select="."/&gt;
      &lt;/xsl:attribute&gt;
    &lt;/xsl:template&gt;
  
    &lt;!--&lt;xsl:template match="@*[local-name(.)='ns0']"/&gt;--&gt;
  
      &lt;xsl:template match="ns0:Cedent/ns0:Party/ns0:Id[. = '']"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates select="@*" /&gt;
        &lt;xsl:apply-templates select="../../following-sibling::ns0:Broker[1]/ns0:Party/ns0:Id/node()" /&gt;
      &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
  
     &lt;xsl:template match="ns0:Cedent/ns0:Party/ns0:Id/@Agency[. = '']"&gt;
      &lt;xsl:attribute name="Agency"&gt;DUNS_dun_and_bradstreet&lt;/xsl:attribute&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="ns0:Reinsurer[not(ns0:Party/ns0:Id and ns0:Party/ns0:Id/@Agency)]" /&gt;
  
    &lt;xsl:template match="ns0:Reinsurer/ns0:Contact[not(ns0:PersonName)]" /&gt;
  
    &lt;xsl:template match="ns0:Reinsurer/ns0:Contact/*[not(node())]" /&gt;
  
    &lt;xsl:template match="ns0:Broker/ns0:Contact/ns0:Telephone[.='']" /&gt;
  
    &lt;xsl:template match="ns0:ServiceProvider[. = '6']" /&gt;
  
    &lt;xsl:template match="ns0:ServiceProvider[not(ns0:Party/ns0:Id and ns0:Party/ns0:Id/@Agency)]" /&gt;
  
    &lt;xsl:template match="ns0:Contract/ns0:ContractGroupName[not(node())]" /&gt;
  
    &lt;xsl:template match="ns0:Endorsement[ns0:Placing/ns0:PlacingStage = 'endorsement']" /&gt;
  
    &lt;xsl:template match="ns0:Endorsement/ns0:EndorsementReference[not(node())]" /&gt;
  
    &lt;xsl:template match="ns0:Endorsement/ns0:EndorsementName[not(node())]" /&gt;
  
    &lt;xsl:template match="ns0:Endorsement/ns0:Description[not(node())]" /&gt;
  
    &lt;xsl:template match="ns0:Endorsement/ns0:EffectiveDate[not(node())]" /&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:BrokerSharePercentage[not(node()) and ns0:ContractSection/ns0:BrokerSharePercentage/ns0:Rate &gt; 0]" /&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:RiskLocation[not(node()) or (ns0:PlacingTransactionFunction = 'request_for_line_or_binder' or ns0:PlacingTransactionFunction = 'signed_line_advice' or ns0:PlacingTransactionFunction = 'quotation_request' or ns0:PlacingTransactionFunction = 'endorsement_request')]" /&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:RiskLocation/ns0:Location/ns0:Supraentity[not(node())]" /&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:RiskLocation/ns0:Location/ns0:Country[not(node())]" /&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:PerilsIncluded[not(ns0:Peril/ns0:PerilType !='')]" /&gt;
  
    &lt;xsl:template match="ns0:OrderPercentage"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates/&gt;
      &lt;/xsl:copy&gt;
      &lt;ns0:LinesPercentageOfOrderIndicator&gt;
        &lt;xsl:value-of select="ancestor::ns0:Placing/ns0:PlacingTransactionFunction = 'signed_line_advice' and ns0:Rate &gt; 100"/&gt;
      &lt;/ns0:LinesPercentageOfOrderIndicator&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:Brokerage[descendant::ns0:Rate = '' and not(ns0:ContractSection/ns0:BrokeragePercentage/ns0:Rate &gt; 0)]" /&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:Reinsurer[descendant::ns0:Id = '' and not(ns0:Party/ns0:Id/@Agency[. != ''])]" /&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerContractReference[not(node())]" /&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerQuoteMaximumSharePercentage[descendant::ns0:Rate = '']" /&gt;
  
    &lt;xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerWrittenPercentage[descendant::ns0:Rate = '' and ns0:PlacingTransactionFunction = 'signed_line_advice']" /&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>Sample Input:</strong></p>
  
  <pre><code>&lt;ns0:Root xmlns:ns0="http://somenamespace"&gt;
      &lt;ns0:Placing Sender="broker" Receiver="serviceprovider"&gt;
          &lt;ns0:UUId&gt;GUID&lt;/ns0:UUId&gt;
          &lt;ns0:BrokerReference&gt;2B3B8992-3185-48EE-A030-0F61EFF7C7EB&lt;/ns0:BrokerReference&gt;
          &lt;ns0:ServiceProviderReference&gt;16&lt;/ns0:ServiceProviderReference&gt;
          &lt;ns0:PlacingStage&gt;order&lt;/ns0:PlacingStage&gt;
          &lt;ns0:PlacingTransactionFunction&gt;signed_line_advice&lt;/ns0:PlacingTransactionFunction&gt;
          &lt;ns0:TransactionReasonDescription&gt;&lt;/ns0:TransactionReasonDescription&gt;
          &lt;ns0:Cedent&gt;
              &lt;ns0:Party&gt;
                  &lt;ns0:Id Agency=""&gt;&lt;/ns0:Id&gt;
                  &lt;ns0:Name&gt;Bahamas First General Insurance Co/Aon Benfield Canada&lt;/ns0:Name&gt;
              &lt;/ns0:Party&gt;
          &lt;/ns0:Cedent&gt;
          &lt;ns0:Reinsurer&gt;
              &lt;ns0:Party&gt;
                  &lt;ns0:Id Agency=""&gt;&lt;/ns0:Id&gt;
                  &lt;ns0:Name&gt;RI3K&lt;/ns0:Name&gt;
              &lt;/ns0:Party&gt;
              &lt;ns0:Contact&gt;
                  &lt;ns0:PersonName&gt;test&lt;/ns0:PersonName&gt;
                  &lt;ns0:Telephone&gt;&lt;/ns0:Telephone&gt;
                  &lt;ns0:Email&gt;test@ri3k.com&lt;/ns0:Email&gt;
              &lt;/ns0:Contact&gt;
          &lt;/ns0:Reinsurer&gt;
          &lt;ns0:Broker&gt;
              &lt;ns0:Party&gt;
                  &lt;ns0:Id Agency="DUNS_dun_and_bradstreet"&gt;292320710&lt;/ns0:Id&gt;
                  &lt;ns0:Name&gt;Aon Benfield UK&lt;/ns0:Name&gt;
              &lt;/ns0:Party&gt;
              &lt;ns0:Contact&gt;
                  &lt;ns0:PersonName&gt;Jenny Edwards&lt;/ns0:PersonName&gt;
                  &lt;ns0:Telephone&gt;reinsurance_contract&lt;/ns0:Telephone&gt;
                  &lt;ns0:Email&gt;jenny.edwards@aonbenfield.com&lt;/ns0:Email&gt;
              &lt;/ns0:Contact&gt;
          &lt;/ns0:Broker&gt;
          &lt;ns0:ServiceProvider&gt;
              &lt;ns0:Party&gt;
                  &lt;ns0:Id Agency="DUNS_dun_and_bradstreet"&gt;239195295&lt;/ns0:Id&gt;
              &lt;/ns0:Party&gt;
          &lt;/ns0:ServiceProvider&gt;
          &lt;ns0:Contract&gt;
              &lt;ns0:ContractName&gt;FINCO QUOTA SHARE TREATY&lt;/ns0:ContractName&gt;
              &lt;ns0:ContractGroupName&gt;BFG FINCO QUOTA SHARE TREATY&lt;/ns0:ContractGroupName&gt;
              &lt;ns0:ContractType&gt;0&lt;/ns0:ContractType&gt;
              &lt;ns0:BrokerReference&gt;B110813BDO1053&lt;/ns0:BrokerReference&gt;
              &lt;ns0:BrokerGroupReference&gt;200153436&lt;/ns0:BrokerGroupReference&gt;
              &lt;ns0:BrokerRiskReference&gt;13BDO1053&lt;/ns0:BrokerRiskReference&gt;
          &lt;/ns0:Contract&gt;
          &lt;ns0:Endorsement&gt;
              &lt;ns0:EndorsementReference&gt;0&lt;/ns0:EndorsementReference&gt;
              &lt;ns0:EndorsementName&gt;&lt;/ns0:EndorsementName&gt;
              &lt;ns0:Description&gt;&lt;/ns0:Description&gt;
              &lt;ns0:EffectiveDate&gt;&lt;/ns0:EffectiveDate&gt;
          &lt;/ns0:Endorsement&gt;
          &lt;ns0:ContractSection ContractReportingLevel="section_level"&gt;
              &lt;ns0:HighLevelReference&gt;01&lt;/ns0:HighLevelReference&gt;
              &lt;ns0:CoverType&gt;quota_share&lt;/ns0:CoverType&gt;
              &lt;ns0:ContractPeriod&gt;
                  &lt;ns0:StartDate DateIndicator="Jan  1 2013 12:00AM"&gt;&lt;/ns0:StartDate&gt;
                  &lt;ns0:EndDate DateIndicator="Dec 31 2013 12:00AM"&gt;&lt;/ns0:EndDate&gt;
              &lt;/ns0:ContractPeriod&gt;
              &lt;ns0:UnderwritingYear&gt;Jan &lt;/ns0:UnderwritingYear&gt;
              &lt;ns0:ContractSectionClass&gt;
                  &lt;ns0:JvClassOfBusiness&gt;wind_storm_unspecified&lt;/ns0:JvClassOfBusiness&gt;
                  &lt;ns0:ClassOfBusinessDescription&gt;wind_storm_unspecified&lt;/ns0:ClassOfBusinessDescription&gt;
              &lt;/ns0:ContractSectionClass&gt;
              &lt;ns0:RiskLocation&gt;
                  &lt;ns0:Address&gt;
                      &lt;ns0:Country&gt;&lt;/ns0:Country&gt;
                  &lt;/ns0:Address&gt;
                  &lt;ns0:Location&gt;
                      &lt;ns0:Supraentity&gt;&lt;/ns0:Supraentity&gt;
                      &lt;ns0:Country&gt;BS&lt;/ns0:Country&gt;
                  &lt;/ns0:Location&gt;
              &lt;/ns0:RiskLocation&gt;
              &lt;ns0:PerilsIncluded&gt;
                  &lt;ns0:Peril&gt;
                      &lt;ns0:PerilType&gt;&lt;/ns0:PerilType&gt;
                  &lt;/ns0:Peril&gt;
              &lt;/ns0:PerilsIncluded&gt;
              &lt;ns0:ContractAmountsCurrency&gt;
                  &lt;ns0:Ccy&gt;GBP&lt;/ns0:Ccy&gt;
              &lt;/ns0:ContractAmountsCurrency&gt;
              &lt;ns0:BrokerSharePercentage&gt;
                  &lt;ns0:Rate RateUnit="percentage"&gt;2.500000&lt;/ns0:Rate&gt;
              &lt;/ns0:BrokerSharePercentage&gt;
              &lt;ns0:OrderPercentage&gt;
                  &lt;ns0:Rate RateUnit="percentage"&gt;41.000000&lt;/ns0:Rate&gt;
              &lt;/ns0:OrderPercentage&gt;
              &lt;ns0:Brokerage&gt;
                  &lt;ns0:BrokeragePercentage&gt;
                      &lt;ns0:Rate RateUnit="percentage"&gt;2.500000&lt;/ns0:Rate&gt;
                  &lt;/ns0:BrokeragePercentage&gt;
              &lt;/ns0:Brokerage&gt;
              &lt;ns0:ContractMarket&gt;
                  &lt;ns0:Reinsurer&gt;
                      &lt;ns0:Party&gt;
                          &lt;ns0:Id Agency="DUNS_dun_and_bradstreet"&gt;239195295&lt;/ns0:Id&gt;
                      &lt;/ns0:Party&gt;
                  &lt;/ns0:Reinsurer&gt;
                  &lt;ns0:ParticipantFunction&gt;leader&lt;/ns0:ParticipantFunction&gt;
                  &lt;ns0:ReinsurerContractReference&gt;&lt;/ns0:ReinsurerContractReference&gt;
                  &lt;ns0:ReinsurerQuoteMaximumSharePercentage&gt;
                      &lt;ns0:Rate RateUnit="percentage"&gt;100.000000&lt;/ns0:Rate&gt;
                  &lt;/ns0:ReinsurerQuoteMaximumSharePercentage&gt;
                  &lt;ns0:ReinsurerWrittenPercentage&gt;
                      &lt;ns0:Rate RateUnit="percentage"&gt;100.000000&lt;/ns0:Rate&gt;
                  &lt;/ns0:ReinsurerWrittenPercentage&gt;
              &lt;/ns0:ContractMarket&gt;
          &lt;/ns0:ContractSection&gt;
      &lt;/ns0:Placing&gt;
  &lt;/ns0:Root&gt;
  </code></pre>
  
  <p><strong>Expected OutPut:</strong></p>
  
  <pre><code>&lt;Root xmlns="http://somenamespace" xmlns="http://ac.namespace"&gt;
    &lt;Placing xmlns="http://somenamespace" Sender="broker" Receiver="serviceprovider"&gt;
      &lt;UUId&gt;GUID&lt;/UUId&gt;
      &lt;BrokerReference&gt;2B3B8992-3185-48EE-A030-0F61EFF7C7EB&lt;/BrokerReference&gt;
      &lt;ServiceProviderReference&gt;16&lt;/ServiceProviderReference&gt;
      &lt;PlacingStage&gt;order&lt;/PlacingStage&gt;
      &lt;PlacingTransactionFunction&gt;signed_line_advice&lt;/PlacingTransactionFunction&gt;
      &lt;TransactionReasonDescription /&gt;
      &lt;Cedent&gt;
        &lt;Party&gt;
          &lt;Id Agency="DUNS_dun_and_bradstreet" &gt;292320710&lt;/Id&gt;
          &lt;Name&gt;Bahamas First General Insurance Co/Aon Benfield Canada&lt;/Name&gt;
        &lt;/Party&gt;
      &lt;/Cedent&gt;
      &lt;Reinsurer&gt;
        &lt;Party&gt;
          &lt;Id Agency="" /&gt;
          &lt;Name&gt;RI3K&lt;/Name&gt;
        &lt;/Party&gt;
        &lt;Contact&gt;
          &lt;PersonName&gt;test&lt;/PersonName&gt;
          &lt;Email&gt;test@ri3k.com&lt;/Email&gt;
        &lt;/Contact&gt;
      &lt;/Reinsurer&gt;
      &lt;Broker&gt;
        &lt;Party&gt;
          &lt;Id Agency="DUNS_dun_and_bradstreet"&gt;292320710&lt;/Id&gt;
          &lt;Name&gt;Aon Benfield UK&lt;/Name&gt;
        &lt;/Party&gt;
        &lt;Contact&gt;
          &lt;PersonName&gt;Jenny Edwards&lt;/PersonName&gt;
          &lt;Telephone&gt;reinsurance_contract&lt;/Telephone&gt;
          &lt;Email&gt;jenny.edwards@aonbenfield.com&lt;/Email&gt;
        &lt;/Contact&gt;
      &lt;/Broker&gt;
      &lt;ServiceProvider&gt;
        &lt;Party&gt;
          &lt;Id Agency="DUNS_dun_and_bradstreet"&gt;239195295&lt;/Id&gt;
        &lt;/Party&gt;
      &lt;/ServiceProvider&gt;
      &lt;Contract&gt;
        &lt;ContractName&gt;FINCO QUOTA SHARE TREATY&lt;/ContractName&gt;
        &lt;ContractGroupName&gt;BFG FINCO QUOTA SHARE TREATY&lt;/ContractGroupName&gt;
        &lt;ContractType&gt;0&lt;/ContractType&gt;
        &lt;BrokerReference&gt;B110813BDO1053&lt;/BrokerReference&gt;
        &lt;BrokerGroupReference&gt;200153436&lt;/BrokerGroupReference&gt;
        &lt;BrokerRiskReference&gt;13BDO1053&lt;/BrokerRiskReference&gt;
      &lt;/Contract&gt;
      &lt;Endorsement&gt;
        &lt;EndorsementReference&gt;0&lt;/EndorsementReference&gt;
      &lt;/Endorsement&gt;
      &lt;ContractSection ContractReportingLevel="section_level"&gt;
        &lt;HighLevelReference&gt;01&lt;/HighLevelReference&gt;
        &lt;CoverType&gt;quota_share&lt;/CoverType&gt;
        &lt;ContractPeriod&gt;
          &lt;StartDate DateIndicator="Jan  1 2013 12:00AM" /&gt;
          &lt;EndDate DateIndicator="Dec 31 2013 12:00AM" /&gt;
        &lt;/ContractPeriod&gt;
        &lt;UnderwritingYear&gt;Jan &lt;/UnderwritingYear&gt;
        &lt;ContractSectionClass&gt;
          &lt;JvClassOfBusiness&gt;wind_storm_unspecified&lt;/JvClassOfBusiness&gt;
          &lt;ClassOfBusinessDescription&gt;wind_storm_unspecified&lt;/ClassOfBusinessDescription&gt;
        &lt;/ContractSectionClass&gt;
        &lt;RiskLocation&gt;
          &lt;Address&gt;
            &lt;Country /&gt;
          &lt;/Address&gt;
          &lt;Location&gt;
            &lt;Country&gt;BS&lt;/Country&gt;
          &lt;/Location&gt;
        &lt;/RiskLocation&gt;
        &lt;ContractAmountsCurrency&gt;
          &lt;Ccy&gt;GBP&lt;/Ccy&gt;
        &lt;/ContractAmountsCurrency&gt;
        &lt;BrokerSharePercentage&gt;
          &lt;Rate RateUnit="percentage"&gt;2.500000&lt;/Rate&gt;
        &lt;/BrokerSharePercentage&gt;
        &lt;OrderPercentage &gt;
          &lt;Rate RateUnit="percentage"&gt;41.000000&lt;/Rate&gt;
        &lt;/OrderPercentage&gt;
        &lt;LinesPercentageOfOrderIndicator&gt;false&lt;/LinesPercentageOfOrderIndicator&gt;
        &lt;Brokerage&gt;
          &lt;BrokeragePercentage&gt;
            &lt;Rate RateUnit="percentage"&gt;2.500000&lt;/Rate&gt;
          &lt;/BrokeragePercentage&gt;
        &lt;/Brokerage&gt;
        &lt;ContractMarket&gt;
          &lt;Reinsurer&gt;
            &lt;Party&gt;
              &lt;Id Agency="DUNS_dun_and_bradstreet"&gt;239195295&lt;/Id&gt;
            &lt;/Party&gt;
          &lt;/Reinsurer&gt;
          &lt;ParticipantFunction&gt;leader&lt;/ParticipantFunction&gt;
          &lt;ReinsurerQuoteMaximumSharePercentage&gt;
            &lt;Rate RateUnit="percentage"&gt;100.000000&lt;/Rate&gt;
          &lt;/ReinsurerQuoteMaximumSharePercentage&gt;
          &lt;ReinsurerWrittenPercentage&gt;
            &lt;Rate RateUnit="percentage"&gt;100.000000&lt;/Rate&gt;
          &lt;/ReinsurerWrittenPercentage&gt;
        &lt;/ContractMarket&gt;
      &lt;/ContractSection&gt;
    &lt;/Placing&gt;
  &lt;/Root&gt;
  </code></pre>