XSLT 2.0:删除xmlns属性

我有一个问题,Microsoft Excel需要内联格式化文本的特定格式,以便Data元素具有xmlns“ http://www.w3.org/TR/REC-html40 ”,但子元素(Font和B)没有定义的名称空间:

<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> 

实现下面的输出(注意Font和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> 

我知道定义的默认命名空间(urn:schemas-microsoft-com:office:spreadsheet)被添加到Font和B元素来澄清命名空间,因为父项(ss:Data)被定义为在“ http://www.w3.org/TR/REC-html40 ”命名空间,但我的问题是,Microsoft Excel根本不工作,除非ss:Data元素被定义为使用xmlns的“ http://www.w3 .org / TR / REC-html40 “并且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 =“”),而不是完全删除属性。

将名称空间设置为“”不起作用,因为这意味着B元素不再属于任何名称空间,而您确实希望它属于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>