xml - 通过应用 xslt 转换从 wxs 文件中删除不需要的节点

标签 xml xslt wix heat

我正在尝试通过应用 xml 转换来清理由 heat.exe 生成的 wxs 文件。

下面是 heat.exe 的示例文件输出。

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
            <DirectoryRef Id="APPFOLDER">
                <Component Id="cmp78E9FF58917B1844F3E9315A285F3147" Guid="SOME-GUID">
                    <File Id="fil093D6D7CB723B5B62730D7B4E575F154" KeyPath="yes" Source="PQR.Some.dll" />
                </Component>
                <Component Id="cmp0B084126FAE7577FD84DB29766AC6C2B" Guid="SOME-GUID">
                    <File Id="filB20C8708D7EB02EDFBCC4D70F9FE7F8A" KeyPath="yes" Source="ABC.Another.dll" />
                </Component>
                <Component Id="cmp83BB1954DECD7D949AAE4ACA68806EC3" Guid="SOME-GUID">
                    <File Id="fil0E29FBFF7DB39F307A2EE19237A0A579" KeyPath="yes" Source="ABC.OneMore.dll" />
                </Component>
            </DirectoryRef>
        </Fragment>
        <Fragment>
            <ComponentGroup Id="AppFiles">
                <ComponentRef Id="cmp78E9FF58917B1844F3E9315A285F3147" />
                <ComponentRef Id="cmp0B084126FAE7577FD84DB29766AC6C2B" />
                <ComponentRef Id="cmp83BB1954DECD7D949AAE4ACA68806EC3" />
            </ComponentGroup>
        </Fragment>
    </Wix>

我想删除 Component 节点,其子 File 节点的 Source 属性包含字符串“ABC”。我知道如何使用正确的匹配模式找到此类节点。 因此,在删除 Component 节点之前,我想存储组件的 Id,然后使用它来删除具有我刚刚记录的 Id 的 ComponentRef 节点。

有没有一种方法可以通过 XML 转换来实现这一点?我想我正在寻找可以创建变量的东西,比如“X”来存储我删除的组件节点的 ID,并使用“X”来查找要删除的 ComponentRef 节点。

最佳答案

你可以在没有变量的情况下做到这一点。像这样:

<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="*"/>
  <xsl:key name="kCompsToRemove"
           match="wi:Component[contains(wi:File/@Source, 'ABC')]"
           use="@Id" />

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*[self::wi:Component or self::wi:ComponentRef]
                        [key('kCompsToRemove', @Id)]" />
</xsl:stylesheet>

在您的样本输入上运行时,会产生:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Fragment>
    <DirectoryRef Id="APPFOLDER">
      <Component Id="cmp78E9FF58917B1844F3E9315A285F3147" Guid="SOME-GUID">
        <File Id="fil093D6D7CB723B5B62730D7B4E575F154" KeyPath="yes" Source="PQR.Some.dll" />
      </Component>
    </DirectoryRef>
  </Fragment>
  <Fragment>
    <ComponentGroup Id="AppFiles">
      <ComponentRef Id="cmp78E9FF58917B1844F3E9315A285F3147" />
    </ComponentGroup>
  </Fragment>
</Wix>

关于xml - 通过应用 xslt 转换从 wxs 文件中删除不需要的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15305718/

相关文章:

java - JAXB:根据元素值解码到子类

java - 尝试在空对象引用上调用 ScrollView.removeAllViews()

.net - 从 XmlSchema .net 的输出中删除 docType 声明

java - 字体未加载/未找到 Apache fop 2.3

xslt - BizTalk map functoid 与 BizTalk map xslt

wix - 在 WiX 中,如何创建一个对话框来选择主应用程序目录的子文件夹的名称?

image - 使用WiX时,为什么我的MSI图像看起来损坏了?

xml - XSLT/XPath 获取当前节点号

xml - Xpath 选择节点直到标记

wix 在更改/修改期间修改注册表值