tags:

views:

764

answers:

3

I am trying to remove the attribute xmlns="http://webdev2003.test.com" from the following xml using xsl/xslt, a requirement of the XML Task in SSIS. What is a proper methodology considering a large file size. ~40mb

<?xml version="1.0" encoding="utf-16"?> <ArrayOfAccount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
<Account>
<FirstName xmlns="http://webdev2003.test.com/"&gt;John&lt;/FirstName&gt;
<LastName xmlns="http://webdev2003.test.com/"&gt;Smith&lt;/LastName&gt;
</Account>
</ArrayOfAccount>

A: 

What about

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="attribute::*"/>
      <xsl:if test="namespace-uri()!='http://webdev2003.test.com/' and
              namespace-uri()!=''">
        <xsl:attribute name="xmlns">
          <xsl:value-of select="namespace-uri()"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

?

Johannes Weiß
I get an error from XML Notepad - An attribute with a local name 'xmlns' and a null namespace URI cannot be created. Respectively in MSVS error: An attribute with a local name 'xmlns' and a null namespace URI cannot be created.
decompiled
+1  A: 

I hate when I answer my own questions, but the credit goes to - http://blogs.msdn.com/kaevans/archive/2003/06/13/8679.aspx

The first part of the example lists how to remove all attributes which in my scenario works. Perhaps there is a better solution?

decompiled
A: 

I think you can remove the namespace declarations as described in this article. It looks like you might have to declare a prefix for the namespace in your stylesheet before adding it to the exclude-result-prefixes attribute.

You can prevent this from happening with the xsl:stylesheet element's exclude-result-prefixes attribute. This attribute's name can be confusing, because the namespace prefixes will still show up in the result tree. It doesn't mean "exclude the prefixes in the result"; it means "exclude the namespaces with these prefixes".

Don Kirkby