基于其他子节点移除Wix XML中的节点或子节点
XSLT处理Wix XML:基于条件移除节点的实用方案
嘿,我之前刚处理过类似的Wix XML清理需求,结合你的场景,给你几个适合初学者上手的XSLT实现思路,都是基于模板匹配的核心逻辑:
核心思路:身份模板+自定义匹配模板
XSLT的核心是模板匹配,我们先写一个「身份模板」用来复制所有不需要修改的节点,然后针对需要移除的节点写自定义模板,让这些节点不被输出(或按需修改后输出)。
场景1:移除包含特定Source的File所在的整个Component
比如你想移除包含XML.dll的Component(也就是Id="3"的那个),可以这么写:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"> <!-- 身份模板:复制所有未被自定义模板匹配的节点 --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- 匹配包含Source为XML.dll的File的Component,不输出该Component --> <xsl:template match="wix:Component[wix:File/@Source='XML.dll']"/> </xsl:stylesheet>
场景2:移除Component下的指定子节点(比如RegistryValue)
如果你想保留Component本身,但移除其中的RegistryValue节点(比如只保留Valid.dll对应的Component里的RegistryValue,其他都删掉),可以调整模板:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"> <!-- 身份模板 --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- 匹配Component下的RegistryValue,仅当父Component的File不是Valid.dll时移除 --> <xsl:template match="wix:RegistryValue[../wix:File/@Source!='Valid.dll']"/> </xsl:stylesheet>
场景3:移除特定Id的Component(比如pdb文件的Component)
如果要直接移除Id="2"的Component(包含Valid.pdb的那个),模板会更简单:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"> <!-- 身份模板 --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- 匹配Id为2的Component,不输出 --> <xsl:template match="wix:Component[@Id='2']"/> </xsl:stylesheet>
新手注意事项
- 一定要声明Wix的命名空间(
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"),否则XSLT找不到对应的节点。 - 测试时可以先用本地XML编辑器或者在线XSLT调试工具验证效果,再集成到Wix构建流程里。
- 如果需要更复杂的条件(比如匹配多个Source),可以用
or或者contains()函数,比如match="wix:Component[wix:File/@Source='XML.dll' or wix:File/@Source='Another.dll']"。
内容的提问来源于stack exchange,提问作者Hank
相关产品推荐
相关产品推荐

