XSLT将节点从搜索位置移动到另一个

时间:2018-03-13 14:43:45

标签: xslt

我发现很多样本用于在父节点周围移动节点,或者对它们进行分组但是没有从被搜索的位置获取节点并将其放置在要搜索的位置的另一个节点中。

假设我们有这样的结构:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLFOLDER">
            <Directory Id="dir9AF366290476C56B26201053637AFAA1" Name="Lib">
                <Directory Id="dirC1CF73EF7FB9713B4C9AE6AD5F0D6138" Name="site-packages">
                    <Directory Id="dir4B1B5674B365D05CEA0E3010DE56E3A7" Name="win32">
                        <Component Id="cmp2A1A74B990EA8E9CE27135F83D2DD8EB" Guid="*" Win64="yes">
                            <File KeyPath="yes" Source="$(var.HarvestPath)\Lib\site-packages\win32\pythonservice.exe" Id="fileID_PythonService" />
                        </Component>
                    </Directory>
                </Directory>                
            </Directory>
            <Directory Id="dir33A2F09B010813323099475AA1D7838F" Name="Scripts">
                <Directory Id="dir50CF289B7CF01C2C43A5CDF168EA1A4E" Name="__pycache__" />
            </Directory>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="cgroupPythonEnvService">
            <ComponentRef Id="cmp2A1A74B990EA8E9CE27135F83D2DD8EB" />
        </ComponentGroup>
    </Fragment>
</Wix>

虽然事先不知道真实结构(文件夹名称和文件名除外),但我需要获得此输出:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLFOLDER">
            <Directory Id="dir9AF366290476C56B26201053637AFAA1" Name="Lib">
                <Directory Id="dirC1CF73EF7FB9713B4C9AE6AD5F0D6138" Name="site-packages">
                    <Directory Id="dir4B1B5674B365D05CEA0E3010DE56E3A7" Name="win32">                        
                    </Directory>
                </Directory>                
            </Directory>
            <Directory Id="dir33A2F09B010813323099475AA1D7838F" Name="Scripts">
                <Directory Id="dir50CF289B7CF01C2C43A5CDF168EA1A4E" Name="__pycache__" />
                <Component Id="cmp2A1A74B990EA8E9CE27135F83D2DD8EB" Guid="*" Win64="yes">
                    <File KeyPath="yes" Source="$(var.HarvestPath)\Lib\site-packages\win32\pythonservice.exe" Id="fileID_PythonService" />
                </Component>
            </Directory>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="cgroupPythonEnvService">
            <ComponentRef Id="cmp2A1A74B990EA8E9CE27135F83D2DD8EB" />
        </ComponentGroup>
    </Fragment>
</Wix>

编辑#1

XSLT部分(多次尝试之一):

  <xsl:key name="id_search_pyservice"   match="wix:Component[contains(wix:File/@Source, 'pythonservice.exe')]"  use="ancestor::wix:Component/@Id " />
  <xsl:key name="id_search_targetdir"   match="wix:Directory[contains(@Name, 'Scripts')]"                       use="@Id" />

  <xsl:template match="wix:Directory[key('id_search_targetdir', @Id)]">
    <xsl:copy>
      <xsl:copy-of select="*" />
      <xsl:apply-templates select="wix:Component[key('id_search_pyservice', @Id)]" />
    </xsl:copy>
  </xsl:template>

1 个答案:

答案 0 :(得分:0)

将您的id_search_pyservice密钥更改为:

<xsl:key name="id_search_pyservice"
    match="wix:Component[contains(wix:File/@Source, 'pythonservice.exe')]"
    use="@Id " />

然后,将模板匹配更改为:

<xsl:template match="wix:Directory[key('id_search_targetdir', @Id)]">
    <xsl:copy>
        <!-- copy also the attributes -->
        <xsl:copy-of select="@*" />
        <xsl:copy-of select="*" />
        <xsl:copy-of select="//wix:Component[key('id_search_pyservice', @Id)]" />
    </xsl:copy>
</xsl:template>

然后,为要移动的目标节点创建一个空匹配:

<xsl:template match="wix:Component[contains(wix:File/@Source, 'pythonservice.exe')]"/>

查看实际操作(http://xsltfiddle.liberty-development.net/bFukv8q)。