WiX v5中HarvestDirectory的Transform未生效,求排查方向
从WiX v3迁移到v5.0.1时HarvestDirectory Transform失效问题
我正在将产品安装程序从WiX v3迁移至WiX v5.0.1,目前遇到HarvestDirectory的Transform无法生效的问题。
wixproj中的配置
<ItemGroup> <HarvestDirectory Include="D:\source\repos\src\Client\ProductName\bin\Debug\net8.0-windows7.0\" <ComponentGroupName>MyAppComponents</ComponentGroupName> <DirectoryRefId>INSTALLFOLDER</DirectoryRefId> <SuppressRootDirectory>true</SuppressRootDirectory> <Transform>D:\source\repos\src\Client\Setup2\msi\Exclude.xslt</Transform> </HarvestDirectory> <BindPath Include="D:\source\repos\src\Client\ProductName\bin\Debug\net8.0-windows7.0\" /> </ItemGroup>
Exclude.xslt内容
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wix="http://schemas.microsoft.com/wix/2006/wi" version="1.0"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:strip-space elements="*"/> <!-- Copy all elements by default --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- Change RegistryValue Root from HKCR to HKCU --> <xsl:template match="wix:RegistryValue[@Root='HKCR']"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:attribute name="Root">HKCU</xsl:attribute> </xsl:copy> </xsl:template> </xsl:stylesheet>
问题分析与修复
1. wixproj的XML语法错误
HarvestDirectory元素的Include属性行末尾缺少闭合的>,导致整个元素无法被MSBuild正确解析,Transform配置自然不会生效。
修复:补全闭合符号:
<HarvestDirectory Include="D:\source\repos\src\Client\ProductName\bin\Debug\net8.0-windows7.0\">
2. XSLT模板逻辑错误
原模板中处理RegistryValue时,先执行<xsl:apply-templates select="@*"/>复制所有原有属性(包括原Root="HKCR"),再添加新的Root="HKCU"属性,这会导致XML元素出现重复属性,XML解析器会忽略后续重复属性,因此无法完成修改。
修复:直接匹配并替换Root属性,替换后的XSLT模板如下:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wix="http://schemas.microsoft.com/wix/2006/wi" version="1.0"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:strip-space elements="*"/> <!-- Copy all elements and attributes by default --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- Replace Root attribute from HKCR to HKCU --> <xsl:template match="wix:RegistryValue/@Root[.='HKCR']"> <xsl:attribute name="Root">HKCU</xsl:attribute> </xsl:template> </xsl:stylesheet>
或者另一种写法,复制除原Root外的属性再添加新Root:
<xsl:template match="wix:RegistryValue[@Root='HKCR']"> <xsl:copy> <xsl:attribute name="Root">HKCU</xsl:attribute> <xsl:apply-templates select="@*[name()!='Root']"/> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template>
内容的提问来源于stack exchange,提问作者Umar
相关产品推荐
相关产品推荐

