You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MSBuild中用XSLT转换Wix XML以添加RegistryValue

Wix XML转换的XSLT实现(适配MSBuild TransformXml任务)

需求

通过XSLT配合MSBuild的<TransformXml>任务完成以下Wix XML修改:

  • 为每个<Component>元素内部、<File>元素之前插入指定的<RegistryValue>元素
  • 移除所有<File>元素的KeyPath="yes"属性
  • 保留原XML的其余所有内容不变

输入XML

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLFOLDER">
            <Component Id="cmp3E8A66A51C067CB532D96BDB574D5B62" Guid="{7DF9EBF3-A8FF-4A1D-BEEB-6A84A31A5484}">
                <File Id="fil2934E863A1FB7CCB3F2B4AE5D7638C21" KeyPath="yes" Source="test.json" />
            </Component>
            <Component Id="cmpAC81A7A6F463ADCB651789415E02A5EB" Guid="{67BA41FF-FF45-436B-9DF3-C311B061485B}">
                <File Id="fil9E171FDD3F525EDE194757D2A95D1311" KeyPath="yes" Source="test2.json" />
            </Component>
        </DirectoryRef>
    </Fragment>
</Wix>

预期输出XML

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLFOLDER">
            <Component Id="cmp3E8A66A51C067CB532D96BDB574D5B62" Guid="{7DF9EBF3-A8FF-4A1D-BEEB-6A84A31A5484}">
                <RegistryValue Root="HKCU" Key="Key" Name="Installed" Type="integer" Value="1" KeyPath="yes"/>
                <File Id="fil2934E863A1FB7CCB3F2B4AE5D7638C21" Source="test.json" />
            </Component>
            <Component Id="cmpAC81A7A6F463ADCB651789415E02A5EB" Guid="{67BA41FF-FF45-436B-9DF3-C311B061485B}">
                <RegistryValue Root="HKCU" Key="Key" Name="Installed" Type="integer" Value="1" KeyPath="yes"/>
                <File Id="fil9E171FDD3F525EDE194757D2A95D1311" Source="test2.json" />
            </Component>
        </DirectoryRef>
    </Fragment>
</Wix>

实现的XSLT代码

<?xml version="1.0" encoding="utf-8"?>
<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>

    <!-- 处理Component元素:先插入RegistryValue,再处理子元素 -->
    <xsl:template match="wi:Component">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <!-- 插入指定的RegistryValue元素 -->
            <wi:RegistryValue Root="HKCU" Key="Key" Name="Installed" Type="integer" Value="1" KeyPath="yes"/>
            <!-- 处理Component下的所有子元素 -->
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- 处理File元素:移除KeyPath="yes"属性 -->
    <xsl:template match="wi:File/@KeyPath[.='yes']"/>
</xsl:stylesheet>

代码说明

  1. 身份模板:确保原XML的所有节点、属性和文本内容都被原样复制,仅修改需要调整的部分。
  2. Component元素处理:匹配Wix命名空间下的<Component>元素,先复制其自身属性,然后插入指定的<RegistryValue>,最后处理原有的子元素(如<File>)。
  3. File元素属性处理:匹配<File>元素中值为yes的KeyPath属性,通过空模板将其移除。

内容的提问来源于stack exchange,提问作者Nicola Zreinh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 02:52:35