2014-09-27 30 views
1

我在WIX安裝中使用收穫文件文件上的xslt變換。我試圖將某些目錄中的所有文件標記爲永久文件。爲了做到這一點,我必須做一個包含,以查看文件夾名稱是否在源屬性中。下面是節點之一,然後是變換。任何幫助將不勝感激。XSLT:根據包含特定字符串的子屬性值將屬性添加到父項

<Component Id="cmpE4293ADC65367393D7A7630023A43F89" Directory="dirAFEA15D2A28EA2E6080FAD1EE1935E0A" Guid="{691DB98F-E5F4-4979-B2E5-63E14AF8A328}"> 
     <File Id="filE11EC7DCDC230815BECFE0925B1F3DC4" KeyPath="yes" Source="$(var.publishDir)\WebConfig\appSettings.config" /> 
</Component> 

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:wix="http://schemas.microsoft.com/wix/2006/wi" 
    xmlns="http://schemas.microsoft.com/wix/2006/wi" 
    xmlns:msxml="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="wix"> 

    <xsl:template match="wix:Wix"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*" /> 
     <xsl:apply-templates /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="wix:Component"> 

    <!-- Just copy the tag itself --> 
    <xsl:copy> 

     <xsl:variable name="fvsys" > 
     <xsl:value-of select="node()/File/@Source"/> 
     </xsl:variable> 
     <!-- Copy all attributes --> 
     <xsl:apply-templates select="@*" /> 

     <!-- Here comes the distinction: if you find our special component, do some special things --> 
     <xsl:choose> 
     <!-- Note that the string is translated to all lower case, so you don't have to care about being case sensitive or not --> 
     <xsl:when test="contains($fvsys, 'WebConfig')"> 
      <!-- Here we will add the Permanent-attribute to this very special component --> 
      <xsl:attribute name="Permanent">yes</xsl:attribute> 
     </xsl:when> 
     </xsl:choose> 
     <xsl:choose> 
     <!-- Note that the string is translated to all lower case, so you don't have to care about being case sensitive or not --> 
     <xsl:when test="contains($fvsys, 'DocumentConversions')"> 
      <!-- Here we will add the Permanent-attribute to this very special component --> 
      <xsl:attribute name="Permanent">yes</xsl:attribute> 
     </xsl:when> 
     </xsl:choose> 


     <!-- Now take the rest of the inner tag --> 
     <xsl:apply-templates select="node()" /> 
    </xsl:copy> 

    </xsl:template> 

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

</xsl:stylesheet> 

回答

2

我假設你要添加的PermanentComponent節點,而不是在File節點?

如果是這樣,問題是你如何定義fvsys變量:

<xsl:variable name="fvsys" > 
    <xsl:value-of select="node()/File/@Source"/> 
    </xsl:variable> 

這裏有兩個問題。首先,因爲你已經定位在一個Component節點上,所以這將尋找一個File節點,這是一個「大孩子」,而不是一個直接的孩子。其次,它看起來像File節點也是wix命名空間的一部分,所以還需要包含前綴。

試試這個

<xsl:variable name="fvsys" select="wiz:File/@Source" /> 

注意的變量本身使用select的。在您的原始版本中,您將創建屬性值的副本,但在後者聲明中,它仍然直接引用該屬性。

+0

感謝您的回答,我使用了下面列出的內容。 – 2014-09-29 11:15:00

1

感謝您的答案我剛剛發現一個在這裏工作的是我的轉換,這符合我的需求。它將文件夾內的每個文件都標記爲永久文件。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:wix="http://schemas.microsoft.com/wix/2006/wi" 
    xmlns="http://schemas.microsoft.com/wix/2006/wi" 
    xmlns:msxml="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="wix"> 

    <xsl:template match="wix:Wix"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*" /> 
     <xsl:apply-templates /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="wix:Component"> 

    <!-- Just copy the tag itself --> 
    <xsl:copy> 

     <xsl:variable name="fvsys" > 
     <xsl:value-of select="*[local-name()='File']/@Source"/> 
     </xsl:variable> 
     <!-- Copy all attributes --> 
     <xsl:apply-templates select="@*" /> 

     <!-- This will mark all files in the WebConfig folder as permanent --> 
     <xsl:choose> 
     <!-- Note that the string is translated to all lower case, so you don't have to care about being case sensitive or not --> 
     <xsl:when test="contains($fvsys, 'WebConfig\')"> 
      <!-- Here we will add the Permanent-attribute to this very special component --> 
      <xsl:attribute name="Permanent">yes</xsl:attribute> 
     </xsl:when> 
     </xsl:choose> 
     <xsl:choose> 
     <!-- This will mark all files in the DocumentConversions folder as permanent --> 
     <xsl:when test="contains($fvsys, 'DocumentConversions\')"> 
      <!-- Here we will add the Permanent-attribute to this very special component --> 
      <xsl:attribute name="Permanent">yes</xsl:attribute> 
     </xsl:when> 
     </xsl:choose> 


     <!-- Now take the rest of the inner tag --> 
     <xsl:apply-templates select="node()" /> 
    </xsl:copy> 

    </xsl:template> 

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

</xsl:stylesheet>