XSL模板复制和添加元素

时间:2014-07-06 15:48:11

标签: xml xslt xslt-1.0

<ComponentGroup Id="SimulatorComponentGroup">
   <Component Id="cmpCAB8CD4B3E3F5DE9BD27E4BE2C6D4ED5" Directory="Simulator" Guid="*">
       <File Id="fil763F3807501181AEBB3384E197DA1B60" KeyPath="yes" Source="$(var.SimulatorSourcePath)\aeStatGridWeights.txt" />
   </Component>
   <Component Id="cmp9FA0A11B61A218ED2C433E82749C7264" Directory="Simulator" Guid="*">
       <File Id="fil52CCB4416F79DAB20B21723321A693FD" KeyPath="yes" Source="$(var.SimulatorSourcePath)\afStatGridWeights.txt" />
   </Component>
   <Component Id="cmp8EFEB6EE1903B8CF488FED2D3754A8CF" Directory="Simulator" Guid="*">
       <File Id="fil035410628EFD654283E0E6A32D1985C4" KeyPath="yes" Source="$(var.SimulatorSourcePath)\anr_black_ag1_dl65_p80_sc1.ate_config" />
   </Component>

我想创建一个输出以下内容的模板:

<ComponentGroup Id="SimulatorComponentGroup">
    <Component Id="cmpCAB8CD4B3E3F5DE9BD27E4BE2C6D4ED5" Directory="Simulator" Guid="*">
        <File Id="fil763F3807501181AEBB3384E197DA1B60" KeyPath="yes" Source="$(var.SimulatorSourcePath)\aeStatGridWeights.txt" />
        <RemoveFolder Id="cmpCAB8CD4B3E3F5DE9BD27E4BE2C6D4ED5" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\[Manufacturer]\[ProductName]" Type="string" Value="" KeyPath="yes" />
    </Component>
    <Component Id="cmp9FA0A11B61A218ED2C433E82749C7264" Directory="Simulator" Guid="*">
        <RemoveFolder Id="cmp9FA0A11B61A218ED2C433E82749C7264" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\[Manufacturer]\[ProductName]" Type="string" Value="" KeyPath="yes" />
        <File Id="fil52CCB4416F79DAB20B21723321A693FD" KeyPath="yes" Source="$(var.SimulatorSourcePath)\afStatGridWeights.txt" />
    </Component>
    <Component Id="cmp8EFEB6EE1903B8CF488FED2D3754A8CF" Directory="Simulator" Guid="*">
        <RemoveFolder Id="cmp8EFEB6EE1903B8CF488FED2D3754A8CF" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\[Manufacturer]\[ProductName]" Type="string" Value="" KeyPath="yes" />
        <File Id="fil035410628EFD654283E0E6A32D1985C4" KeyPath="yes" Source="$(var.SimulatorSourcePath)\anr_black_ag1_dl65_p80_sc1.ate_config" />
    </Component>

这是我的* .xsl文件:

 <?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">


  <xsl:output omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>
  <xsl:strip-space  elements="*"/>


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

  <xsl:output method="xml" indent="yes" />


 <xsl:template match="wix:Component[@Directory='Simulator']">
    <xsl:copy>
      <xsl:attribute name="Id">
        <xsl:value-of select="./Id"/>
      </xsl:attribute>
      <xsl:apply-templates/>
      <RemoveFolder Id=""></RemoveFolder>
      <RegistryValue Root="HKCU" Key="Software\[Manufacturer]\[ProductName]" Type="string" Value="" KeyPath="yes" />      
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

当我使用副本时,我从“组件”属性中删除 ID 目录,以及如何将Compnent Id=" ID复制到{ {1}}

1 个答案:

答案 0 :(得分:2)

我认为你的问题很清楚,但也许这可以让你走上正确的道路:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Component">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <RemoveFolder Id="{@Id}" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\[Manufacturer]\[ProductName]" Type="string" Value="" KeyPath="yes" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

请注意模板匹配&#34;组件&#34;:

  1. 应用模板复制现有属性和子节点;
  2. 使用属性值模板将Component / @ Id中的值转换为RemoveFolder / @ Id。

  3. 编辑:

    要在{/ strong> <File><RemoveFolder>之后放置<RegistryValue>(或任何现有子节点),请将第二个模板更改为:

    <xsl:template match="Component">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <RemoveFolder Id="{@Id}" On="uninstall" />
            <RegistryValue Root="HKCU" Key="Software\[Manufacturer]\[ProductName]" Type="string" Value="" KeyPath="yes" />
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>