从输出中省略不需要的命名空间

Pet*_*uss 7 xslt xml-namespaces output

我的XSLT是带有xmlns:x="http://something"属性的一些标签...如何避免这个冗余属性?输出XML从不使用,既不在x:tag,也不在x:attribute.


XML示例:

<root><p>Hello</p><p>world</p></root>
Run Code Online (Sandbox Code Playgroud)

XSL的例子:

<xsl:transform version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:output encoding="UTF-8" method="xml" version="1.0" indent="no"/>

<xsl:template match="root"><foo>
   <xsl:for-each select="p">
    <p><xsl:value-of select="." /></p>
   </xsl:for-each></foo>
   <xsl:for-each select="x">
    <link xlink:href="{x}" />
   </xsl:for-each></foo>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

XML输出示例:

<foo>
   <p xmlns:xlink="http://www.w3.org/1999/xlink">Hello</p>
   <p xmlns:xlink="http://www.w3.org/1999/xlink">world</p>
</foo>
Run Code Online (Sandbox Code Playgroud)

xmlns:xlink是一个开销,它没有被使用!


XSLT必须使用命名空间但输出不是的典型情况:

 <xsl:value-of select="php:function('regFunction', . )" />
Run Code Online (Sandbox Code Playgroud)

JLR*_*she 7

正如Dimitre已经说过的,如果你没有xlink在XSLT中的任何地方使用命名空间,你应该删除它的命名空间声明.但是,如果您的XSLT实际上在某个您未向我们展示的地方使用它,则可以通过使用以下exclude-result-prefixes属性来阻止它的输出:

<xsl:transform version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   exclude-result-prefixes="xlink">
Run Code Online (Sandbox Code Playgroud)