XSLT:向根元素添加命名空间声明 [英] XSLT : Add a namespace declaration to the root element
问题描述
我有这个 XML 文档:
I have this XML document :
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="AcquisitionFolder">
<Directory Id="dir2EE87E668A6861A2C8B6528214144568" Name="bin" />
<Directory Id="dir99C9EB95694B90A2CD31AD7E2F4BF7F6" Name="Decoders" />
</DirectoryRef>
</Fragment>
</Wix>
我想获得以下结果:
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension>
<Fragment>
<DirectoryRef Id="AcquisitionFolder">
<Directory Id="dir2EE87E668A6861A2C8B6528214144568" Name="bin" />
<Directory Id="dir99C9EB95694B90A2CD31AD7E2F4BF7F6" Name="Decoders" />
</DirectoryRef>
</Fragment>
</Wix>
这似乎是一个简单的问题,但我没有找到解决方案:-(我做了几次尝试,发现了几个类似的问题(例如:XSLT:将命名空间添加到根元素),但他们没有帮助我.
It seems a simple problem, but I didn't find the solution :-( I made several attempts, and found several similar questions (this one for example : XSLT: Add namespace to root element), but they didn't help me.
感谢您的建议!!!
推荐答案
在许多情况下,您可以简单地将命名空间嵌入到投影的 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
这篇关于XSLT:向根元素添加命名空间声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!