Removing a self referencing node in xml using xsl

2019-07-31 04:26发布

问题:

I'm a XSL newb and have really trouble understanding the syntax. What I'm trying to achieve is to remove a node from the xml and its referenced node. This xml is build by running Heatdirectory, a tool provided with WIX Installer. So everything in the file is created dynamically. The one thing I know about the file, is the name of the file I want to remove the reference to.

This is and example XML:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="InstallFolder">
            <Component Id="cmpAEC985F4FAA50E51011E84E93A32F283" Guid="{3574FFF9-F47D-4A3F-A975-64D76546068A}">
                <File Id="fil9C01928D57F128482FD2A78A209FBF95" KeyPath="yes" Source="$(var.SourcePath)\AppSettings.config" />
            </Component>
            <Component Id="cmp10AE81284551BB54849628AE519965C7" Guid="{CF245728-B329-454E-A305-BE33FC818572}">
                <File Id="fil3F1ECB9AAE6412D0FDF9577B44764BA9" KeyPath="yes" Source="$(var.SourcePath)\CsvHelper.dll" />
            </Component>
            <Component Id="cmp596F6F97E366DDB8E0770EC1550C6CB5" Guid="{D8E5EF9E-CB20-4C40-BD02-D79364EC6518}">
                <File Id="filE48072B3494220F703E4B206DF9D2664" KeyPath="yes" Source="$(var.SourcePath)\CsvHelper.pdb" />
            </Component>
        </DirectoryRef>
    </Fragment>          
    <Fragment>
        <ComponentGroup Id="FileComponents">
            <ComponentRef Id="cmpAEC985F4FAA50E51011E84E93A32F283" />
            <ComponentRef Id="cmp10AE81284551BB54849628AE519965C7" />
            <ComponentRef Id="cmp596F6F97E366DDB8E0770EC1550C6CB5" />          
        </ComponentGroup>
    </Fragment>
</Wix>    

And this is my XSLT that removes the Component that I want to remove.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl"
    xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*"/>
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

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

Here is what i get out of this.

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
   <Fragment>
      <DirectoryRef Id="InstallFolder">
         <Component Id="cmpAEC985F4FAA50E51011E84E93A32F283" Guid="{3574FFF9-F47D-4A3F-A975-64D76546068A}">
            <File Id="fil9C01928D57F128482FD2A78A209FBF95" KeyPath="yes" Source="$(var.SourcePath)\AppSettings.config"/>
         </Component>
         <Component Id="cmp10AE81284551BB54849628AE519965C7" Guid="{CF245728-B329-454E-A305-BE33FC818572}">
            <File Id="fil3F1ECB9AAE6412D0FDF9577B44764BA9" KeyPath="yes" Source="$(var.SourcePath)\CsvHelper.dll"/>
         </Component>
      </DirectoryRef>
   </Fragment>
   <Fragment>
      <ComponentGroup Id="FileComponents">
         <ComponentRef Id="cmpAEC985F4FAA50E51011E84E93A32F283"/>
         <ComponentRef Id="cmp10AE81284551BB54849628AE519965C7"/>
         <ComponentRef Id="cmp596F6F97E366DDB8E0770EC1550C6CB5"/>
      </ComponentGroup>
   </Fragment>
</Wix>

But I aslo want to remove the ComponentRef referencing to the already removed Component. Can anyone tell me how to do this?

PS: I tried to create an variable containing the ID of the Component (since I dont know this, and cannot put this into my XSLT file). But then I could not get to the CompenentGroup and remove the ComponentRef.

回答1:

Store the ID of components that are to be deleted in a variable. Then, you can reference it wherever you need to find a certain component. You rightly guessed that this might be the solution. Here is how you can do it.

I added a second exception (i.e. a template) to the identity transform template that removes the said component reference.

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

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

<xsl:variable name="del-id" select="//wix:Component[contains(wix:File/@Source,'CsvHelper.pdb')]/@Id"/>

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

<xsl:template match="wix:Component[@Id=$del-id]"/>

<xsl:template match="wix:ComponentRef[@Id=$del-id]"/>

</xsl:stylesheet>

EDIT: Here is another solution without using variables in template matches. It's a bit longer though.

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

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

<xsl:variable name="del-id" select="//wix:Component[contains(wix:File/@Source,'CsvHelper.pdb')]/@Id"/>

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

<xsl:template match="wix:ComponentGroup">
  <xsl:copy>
     <xsl:copy-of select="@*"/>
     <xsl:for-each select="wix:ComponentRef">
        <xsl:choose>
           <xsl:when test="@Id=$del-id"/>
           <xsl:otherwise>
              <xsl:copy>
                 <xsl:apply-templates select="node()|@*"/>
              </xsl:copy>
           </xsl:otherwise>
        </xsl:choose>
     </xsl:for-each>
  </xsl:copy>
</xsl:template>

<xsl:template match="wix:DirectoryRef">
  <xsl:copy>
     <xsl:copy-of select="@*"/>
     <xsl:for-each select="wix:Component">
        <xsl:choose>
           <xsl:when test="@Id=$del-id"/>
           <xsl:otherwise>
              <xsl:copy>
                 <xsl:apply-templates select="node()|@*"/>
              </xsl:copy>
           </xsl:otherwise>
        </xsl:choose>
     </xsl:for-each>
     <xsl:for-each select="wix:Directory">
        <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
     </xsl:for-each>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>