XLSX- how to get rid of the default namespace prefix x:?

我是研究僧i 提交于 2019-12-22 04:39:15

问题


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?


回答1:


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>



回答2:


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?



来源:https://stackoverflow.com/questions/1199041/xlsx-how-to-get-rid-of-the-default-namespace-prefix-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!