问题
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>
I'd like to obtain the following result :
<?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>
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.
Thanks for any advice !!!
回答1:
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>
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
来源:https://stackoverflow.com/questions/12055981/xslt-add-a-namespace-declaration-to-the-root-element