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

基于复杂条件移除XML元素:XSLT 1.0处理Wix XML需求

解决Wix XML中移除指定目录组件及对应ComponentRef的XSLT 1.0方案

我明白你已经完成了移除Directory1及其子元素的部分工作,现在需要进一步清理那些和已删除组件对应的ComponentRef元素。咱们来调整你的XSLT代码,实现完整的需求:

首先明确核心需求:

  • 移除DirectoryRef[@Id='Directory1']及其所有子元素
  • 移除所有ComponentRef元素,其Id与Directory1下被删除的Component的Id匹配

以下是调整后的完整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:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- 定义键:收集所有需要保留的Component的Id(即非Directory1下的组件) -->
    <xsl:key name="keep-components" match="wi:DirectoryRef[@Id!='Directory1']/wi:Component" use="@Id"/>

    <!-- 身份模板:默认复制所有节点和属性,是XSLT的基础通用模板 -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- 移除Id为Directory1的DirectoryRef元素及其所有子元素 -->
    <xsl:template match="wi:DirectoryRef[@Id='Directory1']"/>

    <!-- 移除那些不在保留列表中的ComponentRef(即属于已删除的Directory1下的组件引用) -->
    <xsl:template match="wi:ComponentRef[not(key('keep-components', @Id))]"/>
</xsl:stylesheet>

代码逻辑说明:

  1. <xsl:key>的作用:创建一个快速查找的键集合,把所有需要保留的组件ID(也就是Directory2下的Component的Id)存起来,方便后续快速校验ComponentRef是否需要保留。
  2. 移除Directory1节点:通过精准匹配wi:DirectoryRef[@Id='Directory1']并跳过复制操作,直接移除该节点及其所有子元素。
  3. 过滤无效ComponentRef:对每个ComponentRef,检查它的Id是否存在于保留组件的键集合中。如果不存在,说明这个引用对应的组件已经被删除,就跳过复制这个ComponentRef。

这样处理后,就能得到你期望的输出结果:既清理了Directory1及其子组件,也同步移除了对应的无效ComponentRef元素。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:34:19