XSLT 2.0:删除xmlns属性 [英] XSLT 2.0: Remove xmlns attribute

查看:194
本文介绍了XSLT 2.0:删除xmlns属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,其中Microsoft Excel需要一个特定格式的内联格式的文本,使Data元素有一个xmlns的 http://www.w3.org/TR/REC-html40 但是子元素(Font和B)没有定义的命名空间:

 <单元格> 
< ss:Data xmlns =http://www.w3.org/TR/REC-html40ss:Type =Stringxml:space =preserve>
< Font>普通文字< / Font>
< B>粗体文本< / B>
<字体颜色=#FF0000>红色文本< / Font>
< / ss:Data>
< / Cell>

如果将任何xmlns属性添加到Font或B,则它们在Microsoft Excel中无法正确呈现。 / p>

我有以下源XML:

 <?xml版本=1.0encoding =UTF-8?> 
< document>
< text>正常文本< / text>
< b>粗体文本< / b>
< red>红色文字< / red>
< / document>

我的XSLT文件如下:

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

< xsl:stylesheet xmlns:xsl =http://www.w3.org/1999/XSL/Transformxmlns:fo =http://www.w3.org/1999 / XSL / Formatxmlns:xs =http://www.w3.org/2001/XMLSchemaxmlns:x =urn:schemas-microsoft-com:office:excelxmlns:ss =urn:schemas- microsoft-com:office:spreadsheetxmlns:svg =http://www.w3.org/2000/svgversion =2.0xmlns:html =http://www.w3.org/TR/REC -html40xmlns =urn:schemas-microsoft-com:office:spreadsheet>

< xsl:output method =xmlomit-xml-declaration =yesencoding =utf-8version =1.0/>

< xsl:template match =/>

< xsl:value-of disable-output-escaping =yesselect ='& lt;?xml version =& quot; 1.0& quot; encoding =& ; UTF-8&安培; QUOT;?>'/>

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

<工作簿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>
<表>
< Row>
<单元格>
< ss:Data xmlns =http://www.w3.org/TR/REC-html40ss:Type =Stringxml:space =preserve>< xsl: templates select =node()/>< / ss:Data>
< / Cell>
< / Row>
< /表>
< / Worksheet>
< / Workbook>

< / xsl:template>

< xsl:template match =text>
< Font>< xsl:value-of select =。 />< /字体>
< / 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 =。 />< /字体>
< / xsl:template>

< / xsl:stylesheet>

想要实现以下输出(注意字体和B )元素没有xmlns属性:

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

<工作簿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-html40xmlns:fo =http://www.w3.org/1999/XSL/Format
xmlns:xs =http:// www。 w3.org/2001/XMLSchemaxmlns:svg =http://www.w3.org/2000/svg>
< Worksheet ss:Name =Worksheet>
<表>
< Row>
<单元格>
< ss:Data xmlns =http://www.w3.org/TR/REC-html40ss:Type =Stringxml:space =preserve>< Font>正常文本< ; / font>< B>粗体文本< / B><字体颜色=#FF0000>红色文本< / Font> < / SS:数据与GT;
< / Cell>
< / Row>
< /表>
< / Worksheet>
< / Workbook>

相反,我的XSLT变换给了我(注意xmlns已经添加到Font和B元素):

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

<工作簿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-html40xmlns:fo =http://www.w3.org/1999/XSL/Format
xmlns:xs =http:// www。 w3.org/2001/XMLSchemaxmlns:kc =http://www.kelvinconnect.com/
xmlns:svg =http://www.w3.org/2000/svg>
< Worksheet ss:Name =Worksheet>
<表>
< Row>
<单元格>
< ss:Data xmlns =http://www.w3.org/TR/REC-html40ss:Type =Stringxml:space =preserve>< Font xmlns = urn:schemas-microsoft-com:office:spreadsheet>正常文本< / Font>< B xmlns =urn:schemas-microsoft-com:office:spreadsheet>粗体文本< / B><字体颜色=#FF0000xmlns =urn:schemas-microsoft-com:office:spreadsheet> red text< / Font>< / ss:Data>
< / Cell>
< / Row>
< /表>
< / 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和Font元素?



我尝试添加exclude-result-prefixes =#default,但没有效果。



我也试过改变我的模板:

 < xsl:template match =b> 
< xsl:element name =Bnamespace =>
< 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 =Bnamespace =http://www.w3.org/TR/REC-html40>
< xsl:value-of select =。 />
< / xsl:element>
< / xsl:template>

这样做,这意味着红色你必须添加这样的属性

 < xsl:template match =red> 
< xsl:element name =fontnamespace =http://www.w3.org/TR/REC-html40>
< xsl:attribute name =Color>#FF0000< / xsl:attribute>
< xsl:value-of select =。 />
< / xsl:element>
< / xsl:template>


I have a problem where Microsoft Excel wants a specific format for inline formatted text such that the Data element has a xmlns of "http://www.w3.org/TR/REC-html40" but the child elements (Font and B) have no defined namespace:

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

If any xmlns attribute is added to Font or B, they are not rendered correctly in Microsoft Excel.

I have the following source XML:

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

My XSLT file is as follows:

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

I want to achieve the following output (note that the Font and B) elements have no xmlns attribute:

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

Instead, my XSLT transform gives me (note that xmlns has been added to Font and B elements):

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

I understand that the defined default namespace (urn:schemas-microsoft-com:office:spreadsheet) is being added to the Font and B elements to clarify the namespace, given that the parent item (ss:Data) is defined to be in the "http://www.w3.org/TR/REC-html40" namespace, but my problem is that Microsoft Excel simply does not work unless the ss:Data element is defined to use xmlns of "http://www.w3.org/TR/REC-html40" AND the B and Font elements do NOT specify an xmlns attribute.

Can anyone suggest a way to alter my XSLT transform such that xmlns elements are not added to B and Font elements?

I tried adding exclude-result-prefixes="#default", but that had no effect.

I have also tried changing my templates to:

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

But this merely changed the xmlns attribute to blank (xmlns="") rather than removing the attribute completely.

解决方案

Setting the namespace to "" doesn't work because this means the B element no longer belongs to any namespace, when you really want it to belong to the http://www.w3.org/TR/REC-html40 namespace.

Try doing this instead.

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

Doing it this, means for the "red" you would have to add the attribute like this

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

这篇关于XSLT 2.0:删除xmlns属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆