且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XSLT:向根元素添加命名空间声明

更新时间:2022-03-29 02:53:58

在许多情况下,您可以简单地将命名空间嵌入到投影的 Xml 元素(包括根)中作为 文字结果元素:

In many cases, you can simply embed the namespace into the projected Xml elements (including root) as part of a Literal Result Element:

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

    <xsl:template match="/*[local-name()='Wix']">
        <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
             xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
            <xsl:copy-of select="node()|@*"/>
        </Wix>
     </xsl:template>
</xsl:stylesheet>

更正式/一般来说,输出 xml 中的任何命名空间都可以添加到 样式表自己的声明(全局或使用命名空间别名),例如

More formally / generally, any namespaces in your output xml can be added into the stylesheet's own declaration (either globally, or using a namespace alias), e.g.

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wix="http://www.foo.com/2001/v1"
    ...other namespaces here>

... 然后在输出中引用

... and then referenced in the output

<xsl:template match="/">
    <wix:Wix>
       <wix:Child>
          ...

如果在结果输出中残留不需要的/未使用的命名空间(例如,在源文档中需要,但在输出文档中不需要),您可以使用 排除结果前缀

If there are unwanted / unused namespaces residual in the resultant output (e.g. needed in the source document, but not in the output document), you are able able to clean these out with exclude-result-prefixes