如何用XSLT为Wix中所有<File>元素添加Permenant="yes"属性?
为Wix XML的所有元素添加Permanent="yes"属性的正确XSLT实现
你的XSLT未生效的核心原因是没正确处理Wix XML的命名空间——Wix文档中的所有元素都属于http://schemas.microsoft.com/wix/2006/wi命名空间,必须在XSLT中明确绑定并引用这个命名空间才能匹配到
正确的XSLT代码
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wi="http://schemas.microsoft.com/wix/2006/wi"> <!-- 身份模板:原样复制文档中未被专门匹配的所有节点和属性 --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- 匹配Wix命名空间下的<File>元素,添加指定属性 --> <xsl:template match="wi:File"> <xsl:copy> <!-- 保留原<File>元素的所有已有属性 --> <xsl:apply-templates select="@*"/> <!-- 添加Permanent="yes"属性 --> <xsl:attribute name="Permanent">yes</xsl:attribute> <!-- 复制<File>元素内部的所有子节点 --> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
关键说明
- 必须通过
xmlns:wi="http://schemas.microsoft.com/wix/2006/wi"声明Wix命名空间,并使用wi:前缀指代该命名空间下的元素,否则无法匹配到目标节点。 - 身份模板是基础,确保原文档的所有其他内容(如
、 、 等)完全保留,不会丢失任何结构或属性。 - 针对
元素的模板会先复制原有属性,再添加新属性,保证原有配置不被覆盖。
示例效果
原Wix输入片段
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Fragment> <Component> <File Source="myapp.exe" /> <File Source="myapp.dll" /> </Component> </Fragment> </Wix>
转换后输出片段
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Fragment> <Component> <File Source="myapp.exe" Permanent="yes" /> <File Source="myapp.dll" Permanent="yes" /> </Component> </Fragment> </Wix>
内容的提问来源于stack exchange,提问作者brunofer42
相关产品推荐
相关产品推荐

