如何编写XSLT过滤WiX中ServiceFiles.wxs的重复组件
解决WiX Heat重复组件过滤问题
我来帮你搞定这个重复组件的过滤问题!其实不用内嵌C#那么麻烦,纯XSLT就能解决——核心思路是让ServiceFilter.xslt直接读取ConfiguratorFiles.wxs的内容,然后对比过滤掉已经存在的组件。
完整的ServiceFilter.xslt方案
直接替换你现有的ServiceFilter.xslt,这段代码整合了过滤重复项、.pdb和.exe的逻辑:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"> <!-- 默认复制所有内容的模板(Identity Template) --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- 加载ConfiguratorFiles.wxs,为所有已存在的文件Source建立索引 --> <xsl:key name="configurator-file-sources" match="document('ConfiguratorFiles.wxs')//wix:File" use="@Source"/> <!-- 移除那些在ConfiguratorFiles.wxs中已存在的组件(通过文件Source匹配) --> <xsl:template match="wix:Component[key('configurator-file-sources', wix:File/@Source)]"/> <!-- 移除指向重复组件的ComponentRef --> <xsl:key name="configurator-component-ids" match="document('ConfiguratorFiles.wxs')//wix:Component" use="@Id"/> <xsl:template match="wix:ComponentRef[key('configurator-component-ids', @Id)]"/> <!-- 原有逻辑:过滤.pdb文件 --> <xsl:key name="service-search" match="wix:Component[contains(wix:File/@Source, '.pdb')]" use="@Id" /> <xsl:template match="wix:Component[key('service-search', @Id)]"/> <xsl:template match="wix:ComponentRef[key('service-search', @Id)]" /> <!-- 原有逻辑:过滤.exe文件 --> <xsl:key name="exe-search" match="wix:Component[contains(wix:File/@Source, '.exe')]" use="@Id"/> <xsl:template match="wix:Component[key('exe-search', @Id)]"/> <xsl:template match="wix:ComponentRef[key('exe-search', @Id)]"/> </xsl:stylesheet>
关键细节说明
document()函数的作用:它让XSLT可以直接加载并读取另一个XML文件(这里就是ConfiguratorFiles.wxs),这样我们就能拿到第一个文件里的所有组件信息。- 路径注意事项:确保
document('ConfiguratorFiles.wxs')里的路径是正确的——如果两个wxs文件在同一目录下,相对路径就没问题;如果不在同一目录,要改成相对或绝对路径(比如document('../Configurator/ConfiguratorFiles.wxs'))。 - 匹配逻辑的可靠性:我用了文件的
@Source属性来判断重复,因为它是指向实际文件的唯一标识,比Component的@Id更可靠(毕竟Id可能被自定义生成)。 - ComponentRef的处理:别忘了同步过滤对应的
ComponentRef,不然会出现引用不存在组件的错误。
为什么不用C#?
纯XSLT方案更轻量,不需要额外嵌入代码,而且WiX的XSLT处理原生支持document()函数,不需要额外配置,运行起来更稳定。
内容的提问来源于stack exchange,提问作者Tobias Theel
相关产品推荐
相关产品推荐

