如何通过命令行将绝对路径传递给 XSLT 1.0 中的参数(使用 msxsl.exe)?

How can I pass an absolute path via the command line to a parameter in XSLT 1.0 (using msxsl.exe)?

我有一个由 WiX 的热实用程序生成的 WXS 文件。我正在尝试使用(现有的)exclusions.xslt 文件对其进行修改,以根据另一个 XML 文件的内容自动排除某些组件(我将调用这个parts.xml)。 xslt 文件目前用于从安装程序中删除一些 components/files/directories,但对于相对静态的列表。

但是,就目前而言,我主要关心的只是让它从参数加载和存储正确的路径并将其内容存储在变量中。

这是应用转换的方式(一旦 WiX 完成将热量收集到文件 "before.wxs"):

msxsl.exe "before.wxs" "exclusions.xslt" -o "after.wxs" srcroot="$(SrcRoot)"

我将 srcroot 作为 XSLT 文件中的一个参数传入,该文件将用于在用户计算机上查找 parts.xml

从命令行获取参数并将所有反斜杠替换为正斜杠:

<!-- Adapted from  and http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx-->
<xsl:param name="srcroot" />
<xsl:variable name="filename">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="concat('file://', $srcroot, '/foo/parts.xml')" />
        <xsl:with-param name="replace" select='\' />
        <xsl:with-param name="by" select='/' />
    </xsl:call-template>
</xsl:variable>
<xsl:variable name="myparts" select="document($filename)" />

我从 here 得到的字符串替换全部是:

<!-- XSL 1.0 string-replace-all, taken from: http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx -->
<xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text"
                select="substring-after($text,$replace)" />
                <xsl:with-param name="replace" select="$replace" />
                <xsl:with-param name="by" select="$by" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

我收到一个无用的错误:

Code: 0x80004005 Unexpected character in query string.

但如果我只删除我发布的代码片段,错误就会消失。我不确定如何对此进行调试并弄清楚发生了什么,但在浏览它之后,我相信我对我认为应该发生的事情有一个根本性的误解,而不仅仅是拼写错误或语法错误。

最终,我希望parts.xml的内容存储在"myparts"中。我在这里错过了什么?

谢谢!

事实证明,我只需要将字符串文字用单引号引起来(反斜杠不需要转义)。另外我的 srcroot 有一个尾部的反斜杠,所以我在这里从一开始就省略了它。这是更新后的代码:

<!-- Adapted from  and http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx-->
<xsl:param name="srcroot" />
<xsl:variable name="filename">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="concat($srcroot, 'foo/parts.xml')" />
        <xsl:with-param name="replace" select="'\'" />
        <xsl:with-param name="by" select="'/'" />
    </xsl:call-template>
</xsl:variable>

至于弄清楚如何测试它的问题,我终于通过执行以下操作来检查文件是否存在:

因为我使用的是 msxsl,所以我决定使用 fileExists 函数的扩展:

<!-- Adapted from https://www.stylusstudio.com/SSDN/default.asp?action=9&read=2993&fid=48 -->
<msxsl:script language="JScript" implements-prefix="user">
<![CDATA[
fso = new ActiveXObject("Scripting.FileSystemObject");
function fileExists(filename){
return fso.FileExists(filename);
}

// With .NET XSLT processor we need to use the Framework API
//function fileExists(filename){
//return System.IO.File.Exists(filename); 
//} 

]]>
</msxsl:script>

在模板中,我做了以下检查:

<xsl:if test="not( function-available('user:fileExists') )">
        <xsl:message terminate="yes">
            <xsl:text>Required facilities are not available.</xsl:text>
        </xsl:message>
</xsl:if>

如果通过了,我将执行以下操作:

<xsl:choose>
    <xsl:when test="user:fileExists(string($filename))">
        <xsl:message terminate="yes">
            <xsl:text>File found! Here:</xsl:text>
            <xsl:copy-of select="$filename"/>
        </xsl:message>
    </xsl:when>
    <xsl:otherwise>
        <xsl:message terminate="yes">
            <xsl:text>File cannot be found:</xsl:text>
            <xsl:copy-of select="$filename"/>
        </xsl:message>
    </xsl:otherwise>
</xsl:choose>

消息不会输出到控制台,除非出于某种原因我设置了 terminate="yes",所以这会停止构建,我必须将其删除,但它帮助我调试并找到了表明 XSLT 文件已成功接收到该参数,并且我能够检查该路径中的文件是否存在。