在XSL中使用父属性值替换属性值

时间:2013-10-17 12:09:24

标签: xml xslt xpath wix xslt-1.0

我需要替换

  • RemoveFolder 元素, Id 属性,值 ZYXYZ123456

将它们替换为各自的/父

  • 目录元素, Id 属性值

输入XML

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:wix="http://wixtoolset.org/schemas/v4/wxs">
    <?include 
        $(sys.CURRENTDIR)Deployment\Data\Statics.wxi
      ?>
    <Fragment>
        <DirectoryRef Id="TARGETFOLDER">

            <Directory Id="dir3927012B444F1DB745782B917DAF8F52" Name="de">
                <Component Id="cmp0B1BB8F3CC5ABCC8B2DC21EFF552B101" Guid="{61855CC1-97FD-4F9A-BE4D-58BB84E3D45F}">
                    <File Id="fil6E782DCD75BB9136587D1C2C3C8405DA" Source="$(var.Fundraisin.TargetDir)\de\System.Windows.Interactivity.resources.dll" />
                    <RemoveFolder Id="ZYXYZ123456" On="uninstall" />
                    <RegistryValue Root="HKCU" Key="$(var.App.Reg.Path)" Name="deSystemWindowsInteractivityresourcesdllKey" Type="integer" Value="1" KeyPath="yes" />
                </Component>
            </Directory>
            <Directory Id="dir030901E2EAEF85FABAA23B70A484C2DA" Name="en">
                <Component Id="cmp0B23CA905283819B80BD5BCA1DD53351" Guid="{BA8BDBDA-8383-48FC-A72E-DB0BD30F8C0F}">
                    <File Id="filC47344F28A098B48E37BBEDF62663A84" Source="$(var.Fundraisin.TargetDir)\en\System.Windows.Interactivity.resources.dll" />
                    <RemoveFolder Id="ZYXYZ123456" On="uninstall" />
                    <RegistryValue Root="HKCU" Key="$(var.App.Reg.Path)" Name="enSystemWindowsInteractivityresourcesdllKey" Type="integer" Value="1" KeyPath="yes" />
                </Component>
            </Directory>

            <Component Id="cmpA1CA940DDE5CB7E5A98DA7931F3F25F2" Guid="{A6D33CD8-9235-43EC-972C-ED07B3B1E1E8}"><File Id="fil324414361723D2D64834B378C0B7C68B" Source="$(var.Fundraisin.TargetDir)\Castle.Core.dll" /><RegistryValue Root="HKCU" Key="$(var.App.Reg.Path)" Name="CastleCoredllKey" Type="integer" Value="1" KeyPath="yes" /></Component>            
            <Component Id="cmpE41CA83817294A110F385AC18AFC4A10" Guid="{875D5B4F-E03B-4C4D-A14C-494BA2F061DA}"><File Id="filB9649B9DA6A66326E4F8C66B490ED552" Source="$(var.Fundraisin.TargetDir)\Castle.Windsor.dll" /><RegistryValue Root="HKCU" Key="$(var.App.Reg.Path)" Name="CastleWindsordllKey" Type="integer" Value="1" KeyPath="yes" /></Component>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="Dlls">
            <ComponentRef Id="cmpA1CA940DDE5CB7E5A98DA7931F3F25F2" />            
            <ComponentRef Id="cmpE41CA83817294A110F385AC18AFC4A10" />           
            <ComponentRef Id="cmp0B1BB8F3CC5ABCC8B2DC21EFF552B101" />
            <ComponentRef Id="cmp0B23CA905283819B80BD5BCA1DD53351" />
        </ComponentGroup>
    </Fragment>
</Wix>

期望输出的一部分

            ...
            <Directory Id="dir3927012B444F1DB745782B917DAF8F52" Name="de">
                <Component Id="cmp0B1BB8F3CC5ABCC8B2DC21EFF552B101" Guid="{61855CC1-97FD-4F9A-BE4D-58BB84E3D45F}">
                    <File Id="fil6E782DCD75BB9136587D1C2C3C8405DA" Source="$(var.Fundraisin.TargetDir)\de\System.Windows.Interactivity.resources.dll" />
                    <RemoveFolder Id="dir3927012B444F1DB745782B917DAF8F52" On="uninstall" />
                    <RegistryValue Root="HKCU" Key="$(var.App.Reg.Path)" Name="deSystemWindowsInteractivityresourcesdllKey" Type="integer" Value="1" KeyPath="yes" />
                </Component>
            </Directory>
            ...

具有另一个属性值的XSLT替换属性值

how to COPY Attribute value in a new attribute可能是一个开始,但我知道太少的XSLT可以进一步发展它?

1 个答案:

答案 0 :(得分:1)

这个XSLT样式表应该做你想要的。

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:wix="http://wixtoolset.org/schemas/v4/wxs">
  <xsl:output method="xml" indent="yes"/>

  <!-- The identity transform: match nodes and attributes, copy them out and
       do the same to their children i.e. output an identical copy of the input. -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Do something special when we hit a RemoveFolder element with a particular
       Id attribute. The namespace for these elements has to match the one in your
       input XML. -->
  <xsl:template match="wix:RemoveFolder[@Id='ZYXYZ123456']">
    <!-- Copy the element itself. -->
    <xsl:copy>
      <!-- Copy out the original attributes for this element. -->
      <xsl:apply-templates select="@*"/>
      <!-- Output an Id attribute. This will overwrite any existing Id attribute. -->
      <xsl:attribute name="Id">
        <!-- Give it the value of the Id attribute of the Directory element that
             is an ancestor of this RemoveFolder element. -->
        <xsl:value-of select="ancestor::wix:Directory/@Id"/>
      </xsl:attribute>
      <!-- Copy out the children of this element (none in your case, as it happens). -->
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>