首页 > 解决方案 > 在包含反斜杠的“测试”字符串中使用 XSLT 变量

问题描述

我正在尝试编写一个 XSLT 文件以与 Wix Harvest 工具 ( heat.exe ) 一起使用。下面是该工具生成的 XML 的伪代码示例:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="APPLICATIONFOLDER">
            <Component Id="component1" Guid="{guidstring1}">
                <File Id="file1" Source="SourceDir\target.exe" KeyPath="yes" />
            </Component>
            <Component Id="component2" Guid="{guidstring2}">
                <File Id="file2" Source="SourceDir\otherfile.txt" KeyPath="yes" />
            </Component>

我想将一个 Shortcut 子节点添加到具有 Source 属性的文件中,SourceDir\target.exe同时将其他文件的 KeyPath 属性设置为no. (如何为 heat.exe 收集的文件创建快捷方式?被用作此尝试的基础)

以下是我目前拥有的 XSLT 文件的一个片段。该参数targetFile通常是动态设置的,但为了举例,我将其定义为静态字符串。

<xsl:param name="targetFile" select="'target.exe'" />

<xsl:strip-space elements="*"/>

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

<xsl:template match='//wixns:Component/wixns:File[@Source]'>
    <xsl:choose>
        <xsl:when test='not (@Source = SourceDir\target.exe)'>
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:attribute name="KeyPath">
                    <xsl:text>no</xsl:text>
                </xsl:attribute>
            </xsl:copy>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <Shortcut
                    Id="startmenuShortcut"
                    xmlns="http://schemas.microsoft.com/wix/2006/wi"
                    Directory="ProgramMenuDir"
                    WorkingDirectory="APPLICATIONFOLDER"
                    Name="Target"
                    Icon="target.exe"
                    IconIndex="0"
                    Advertise="yes"
                />
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

when当我明确设置测试字符串时,这个 XSLT 文件完成了我想要的。但是,我想利用我在开始时定义的“targetFile”参数。我的第一次尝试是使用以下内容:

<xsl:when test='not (@Source = SourceDir\$targetFile)'>

这导致了错误

Expected token ')', found '\'. not (@Source = SourceDir -->\<-- $targetFile)

{}用like包围变量具有{$targetFile}相似的结果。试图用引号将整个搜索字符串括起来

<xsl:when test='not (@Source = "SourceDir\$targetFile")'>

没有正确识别 File 组件,只是将 KeyPath 属性修改应用于所有内容。

将变量设置为<xsl:param name="targetFile" select="'SourceDir\target.exe'" />,然后将测试设置为

<xsl:when test='not (@Source = $targetFile)'>

导致了正确的修改,但是我想在 XSLT 文件的其他地方使用“target.exe”字符串,所以我想保持变量设置为那个。

在这一点上,我怀疑我对此操作使用了错误的语法,但到目前为止,我试图查找文档以澄清事情的尝试都没有成功。

标签: xslt

解决方案


只需使用该concat功能@Source = concat("SourceDir\", $targetFile)


推荐阅读