2014-04-07 133 views
0

我有一個問題,其中Microsoft Excel需要內聯格式化文本的特定格式,以便Data元素具有xmlns「http://www.w3.org/TR/REC-html40」,但子元素(Font和B)沒有定義的命名空間:XSLT 2.0:刪除xmlns屬性

<Cell> 
    <ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="String" xml:space="preserve"> 
    <Font>normal text</Font> 
    <B>bold text</B> 
    <Font Color="#FF0000">red text</Font> 
    </ss:Data> 
</Cell> 

如果任何xmlns屬性被添加到字體或B,它們不會在Microsoft Excel中正確呈現。

我有以下來源XML:

<?xml version="1.0" encoding="UTF-8"?> 
<document> 
    <text>normal text</text> 
    <b>bold text</b> 
    <red>red text</red> 
</document> 

我的XSLT文件如下:

<?xml version="1.0" encoding="UTF-8"?> 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:svg="http://www.w3.org/2000/svg" version="2.0" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns="urn:schemas-microsoft-com:office:spreadsheet"> 

    <xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" version="1.0"/> 

    <xsl:template match="/"> 

     <xsl:value-of disable-output-escaping="yes" select="'&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>'"/> 

     <xsl:processing-instruction name="mso-application">progid="Excel.Sheet"</xsl:processing-instruction> 

     <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
     xmlns:o="urn:schemas-microsoft-com:office:office" 
     xmlns:x="urn:schemas-microsoft-com:office:excel" 
     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
     xmlns:html="http://www.w3.org/TR/REC-html40"> 

      <Worksheet ss:Name="Worksheet"> 
       <Table> 
        <Row> 
         <Cell> 
          <ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="String" xml:space="preserve"><xsl:apply-templates select="node()" /></ss:Data> 
         </Cell> 
        </Row> 
       </Table> 
      </Worksheet> 
     </Workbook> 

    </xsl:template> 

    <xsl:template match="text"> 
     <Font><xsl:value-of select="." /></Font> 
    </xsl:template> 

    <xsl:template match="b"> 
     <B><xsl:value-of select="." /></B> 
    </xsl:template> 

    <xsl:template match="red"> 
     <Font Color="#FF0000"><xsl:value-of select="." /></Font> 
    </xsl:template> 

</xsl:stylesheet> 

實現下面的輸出(注意字體和B)元素沒有xmlns屬性:

<?xml version="1.0" encoding="utf-8"?><?mso-application progid="Excel.Sheet"?> 

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:fo="http://www.w3.org/1999/XSL/Format" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:svg="http://www.w3.org/2000/svg"> 
    <Worksheet ss:Name="Worksheet"> 
     <Table> 
      <Row> 
       <Cell> 
        <ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="String" xml:space="preserve"><Font>normal text</Font><B>bold text</B><Font Color="#FF0000">red text</Font> </ss:Data> 
       </Cell> 
      </Row> 
     </Table> 
    </Worksheet> 
</Workbook> 

相反,我的XSLT轉換給我(注意的xmlns已添加到[字體和B元素):

<?xml version="1.0" encoding="utf-8"?><?mso-application progid="Excel.Sheet"?> 

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:fo="http://www.w3.org/1999/XSL/Format" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:kc="http://www.kelvinconnect.com/" 
xmlns:svg="http://www.w3.org/2000/svg"> 
    <Worksheet ss:Name="Worksheet"> 
     <Table> 
      <Row> 
       <Cell> 
        <ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="String" xml:space="preserve"><Font xmlns="urn:schemas-microsoft-com:office:spreadsheet">normal text</Font><B xmlns="urn:schemas-microsoft-com:office:spreadsheet">bold text</B><Font Color="#FF0000" xmlns="urn:schemas-microsoft-com:office:spreadsheet">red text</Font></ss:Data> 
       </Cell> 
      </Row> 
     </Table> 
    </Worksheet> 
</Workbook> 

據我所知,所定義的默認命名空間(甕:架構 - 微軟COM:辦公室:電子表格)被添加到假定父項(ss:Data)被定義在「http://www.w3.org/TR/REC-html40」命名空間中,但是我的問題是Microsoft Excel根本無法工作,除非定義了ss:Data元素,否則Font和B元素將闡明命名空間使用「http://www.w3.org/TR/REC-html40」的xmlns,並且B和Font元素不指定xmlns屬性。

任何人都可以提出一種方法來改變我的XSLT轉換,使xmlns元素不會添加到B和字體元素?

我試着添加exclude-result-prefixes =「#default」,但是沒有任何效果。

我也試圖改變我的模板:

<xsl:template match="b"> 
    <xsl:element name="B" namespace=""> 
    <xsl:value-of select="." /> 
    </xsl:element> 
</xsl:template> 

但是,這僅僅是改變了xmlns屬性爲空(的xmlns =「」),而不是完全刪除屬性。

回答

2

將名稱空間設置爲「」不起作用,因爲這意味着當您確實希望它屬於http://www.w3.org/TR/REC-html40名稱空間時,該元素不再屬於任何名稱空間。

請嘗試這樣做。

<xsl:template match="b"> 
    <xsl:element name="B" namespace="http://www.w3.org/TR/REC-html40"> 
    <xsl:value-of select="." /> 
    </xsl:element> 
</xsl:template> 

做這這,意味着爲「紅色」,你就必須添加屬性這樣

<xsl:template match="red"> 
    <xsl:element name="font" namespace="http://www.w3.org/TR/REC-html40"> 
    <xsl:attribute name="Color">#FF0000</xsl:attribute> 
    <xsl:value-of select="." /> 
    </xsl:element> 
</xsl:template> 
+0

謝謝你,這使得完整意義上的和完美的作品! :) –