XSLT:根据包含特定字符串的子属性值将属性添加到父级

时间:2014-09-27 15:43:56

标签: xslt wix wix3.5

我在WIX安装中对收集的文件文件使用xslt转换。我试图将某些目录中的所有文件标记为永久性。为了做到这一点,我必须做一个包含,以查看文件夹名称是否在source属性中。下面是其中一个节点,然后是转换。任何帮助将不胜感激。

<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 个答案:

答案 0 :(得分:3)

我假设您要将Permanent添加到Component节点,而不是File节点?

如果是这样,问题在于如何定义fvsys变量:

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

这里有两个问题。首先,因为您已经定位在Component节点上,所以这将寻找一个File节点,这是一个&#34;大孩子&#34;而不是直接的孩子。其次,看起来File节点也是wix命名空间的一部分,因此还需要包含前缀。

试试这个

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

请注意在变量本身上使用select。在原始版本中,您创建了属性值的副本,但在后一个声明中,它仍然直接引用该属性。

答案 1 :(得分:2)

感谢您的回答我刚刚发现一个可以在这里工作的是我的转换,这符合我的需求。它将文件夹中的每个文件标记为永久文件。

<?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>