7

I'm generating XLSX spreadsheet using OOXML SDK, and I need to get rid of x: namespace prefix. How can I achieve this?

using (SpreadsheetDocument doc = SpreadsheetDocument.Open("template.xlsx", true))
            {
                //Save the shared string table part
                if (doc.WorkbookPart.GetPartsOfType().Count() > 0)
                {
                    SharedStringTablePart shareStringPart = 
doc.WorkbookPart.GetPartsOfType().First(); shareStringPart.SharedStringTable.Save(); } //Save the workbook doc.WorkbookPart.Workbook.Save(); }

Here, the original XLSX file is coming from Excel 2007 and doesn't have the prefix, however, after the save operation the prefix appears. How can I avoid that?

Todd Main
  • 28,951
  • 11
  • 82
  • 146
user116884
  • 81
  • 1
  • 4
  • 1
    Why do you need to get rid of the prefix? What problem is caused by the prefix? – Dirk Vollmar Jul 29 '09 at 09:36
  • Because the client has some weird software which fails to import the file with the prefix - and there is nothing to do with that. I just removed the prefixes manually, and it was imported ok. – user116884 Jul 29 '09 at 09:42
  • How about applying a post-processing step to your XSLX document that removes the prefix and sets the appropriate default namespace? See also http://stackoverflow.com/questions/413050/c-how-to-remove-namespace-information-from-xml-elements/413088#413088 – Dirk Vollmar Jul 29 '09 at 10:01
  • That's can be good, just I need to 'wash' only a specific namespace, and leave all others intact. How can I achieve that? – user116884 Jul 29 '09 at 10:11
  • I actually have the same problem but with Excel 2010. Excel will ignore XmlMaps added to the file if the x: prefix is present. Remove the prefix manually, and it works... – Costo Sep 21 '11 at 02:46

2 Answers2

3

Here is a modified version of the stylesheet linked by divo that strips only a single namespace and copies the rest verbatim:

<xsl:stylesheet version="1.0" xmlns:x="namespace-to-strip" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="no" encoding="UTF-8"/>

  <xsl:template match="/|comment()|processing-instruction()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="x:*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

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

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
Sven Künzler
  • 1,650
  • 1
  • 20
  • 22
0

Unless I'm much mistaken the original file is namespaced as well - only in the form of a default namespace. What's wrong with the namespace in the first place?

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • You're right. That's what I want - the default namespace. That's because my client has some weird software which fails to import the XLSX with the prefix - and there is nothing to do with that. If I remove the prefixes manually, it imports ok. – user116884 Jul 29 '09 at 09:43