2016-01-20 73 views
0

我有一個由WiX的熱工具生成的WXS文件。我試圖用一個(現有的)文件修改它,以便根據另一個XML文件的內容自動排除某些組件(我將稱之爲parts.xml)。 xslt文件目前用於從安裝程序中刪除一些組件/文件/目錄,但是用於相對靜態的列表。如何通過命令行將絕對路徑傳遞給XSLT 1.0中的參數(使用msxsl.exe)?

但是,就目前而言,我主要關心的是如何從參數中加載和存儲正確的路徑,並將其內容存儲在變量中。

這是如何被應用的變換(WiX的一次完成熱收穫到一個文件「before.wxs」):我在srcroot通過

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

是XSLT文件,將內部的參數用於在用戶的機器上找到parts.xml

您可以通過命令行參數,並更換所有回正斜槓斜槓:

<!-- Adapted from http://stackoverflow.com/a/30153713/5605122 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」中。我在這裏錯過了什麼?

謝謝!

+0

你有一個參數'$ src'和另外一個'$ srcroot',如果代碼片段完整的話會給出一個錯誤。 –

+0

哎呀,這實際上是一個錯字,將它複製到這裏(我將其重命名爲使它更清楚它的目的是什麼)。我已經修復了這個問題。實際的文件沒有這個錯字,但我已經把它們全部更改爲「srcroot」。謝謝。事實證明,我也錯過了使用從[這裏](http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx)代碼,我不得不包裝在雙重字符串和單引號。如果有效,我會進行必要的更改和更新。 –

回答

0

事實證明,我只需要在單引號中設置字符串文字(並且不需要轉義反斜槓)。另外我的srcroot有一個尾隨的反斜槓,所以我從一開始就忽略它。以下是更新後的代碼:

<!-- Adapted from http://stackoverflow.com/a/30153713/5605122 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> 

這些消息不輸出到控制檯,除非我設置終止=「是」出於某種原因,所以這次暫停構建和我將不得不刪除它,但它幫助我調試了這一點,並發現該參數已被XSLT文件成功接收,並且我能夠檢查該路徑中的文件是否存在。

相關問題