XSLT not working correctly to remove an element
Given the following XML file:
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="INSTALLLOCATIONAGENT">
<Component Id="cmp140DC3A98DEC1F1F528260F1806A3999" Guid="*">
<File Id="fil57A7BE096A39C9A45B8306DDA29D5B2E" KeyPath="yes" Source="SourceDir\Program.exe" />
</Component>
<Component Id="cmpD2075C288DC3B6998C8A7C7E7DF78EB7" Guid="*">
<File Id="filD8C1A7C27FA3DCB1A863FC3F0420B444" KeyPath="yes" Source="SourceDir\System.Data.SqlServerCe.dll" />
</Component>
<Directory Id="dir1377A3F5233B835FB05556A0B1386070" Name="amd64">
<Component Id="cmp41C2DD9BFF63ABDC86410CFD8251F745" Guid="*">
<File Id="fil87F68441B60D7259CD41A2CDB411AFC6" KeyPath="yes" Source="SourceDir\amd64\sqlcecompact40.dll" />
</Component>
<Component Id="cmpAA3318DF657EDDE7DF58C4CF51DEE2A9" Guid="*">
<File Id="fil97009E86AAFE6C33EF01BF2EBF205249" KeyPath="yes" Source="SourceDir\amd64\sqlceer40EN.dll" />
</Component>
<Component Id="cmp2DD5FBEDECB367F9B56953EBE56D9AEA" Guid="*">
<File Id="fil42F87CDC8C959316EC83D9271E737C38" KeyPath="yes" Source="SourceDir\amd64\sqlceme40.dll" />
</Component>
</Directory>
</Directory>
</DirectoryRef>
</Fragment>
<Fragment>
<ComponentGroup Id="RetailClub.Agent_Project">
<ComponentRef Id="cmpFFCDA0633C7840EACD8A6CEEFA6EC93A" />
<ComponentRef Id="cmp96A706AAAB79BB01C9E42BBE9D4DD252" />
<ComponentRef Id="cmpF2F2F70F2405BCD6BBBB9579CE9EF19B" />
<ComponentRef Id="cmpF292744916F0F54B44B137EF41E6B160" />
<ComponentRef Id="cmpFAA31CC5660D6586E79ECC5D008CB730" />
<ComponentRef Id="cmp9D128034819C12206F13D1EEAABC27A3" />
<ComponentRef Id="cmpA4ED042A16E7DD31342DA0D47DC7E455" />
<ComponentRef Id="cmpD531F67ABC1E63E8DB26A6F38D95B212" />
<ComponentRef Id="cmp7A771F6C7D7E3E3DD707534E56E6A872" />
<ComponentRef Id="cmpC60669855D22EE54FCA4855DF53BA0E7" />
<ComponentRef Id="cmp6A8D269A1F473E6F4B091023BC661C5A" />
<ComponentRef Id="cmpBB8EE28B3ECA91AC087B9D6D365FB19F" />
<ComponentRef Id="cmpA19E7221E858827A2E5A08010A1AB80A" />
<ComponentRef Id="cmp140DC3A98DEC1F1F528260F1806A3999" />
<ComponentRef Id="cmpAC61AD2134F0DA7E4A92C673648AAF1F" />
<ComponentRef Id="cmp71A3488860AA96C23B2B73BE84AD944D" />
<ComponentRef Id="cmpD79B6BCF2C02DFE7B4D089414E982035" />
<ComponentRef Id="cmp6336B810161B473EFA5920B8ACA2101D" />
<ComponentRef Id="cmpD2075C288DC3B6998C8A7C7E7DF78EB7" />
<ComponentRef Id="cmp625073995F3B29478ACC5A45E5005184" />
<ComponentRef Id="cmp6919DDAC4EF84609E800F79B84ACE037" />
<ComponentRef Id="cmpD411331F9F18AC0A62F4D342193C578B" />
<ComponentRef Id="cmp41C2DD9BFF63ABDC86410CFD8251F745" />
<ComponentRef Id="cmpAA3318DF657EDDE7DF58C4CF51DEE2A9" />
<ComponentRef Id="cmp2DD5FBEDECB367F9B56953EBE56D9AEA" />
<ComponentRef Id="cmpD6209C86C787D37B2BC10491715961A4" />
<ComponentRef Id="cmpDEFFF257D183C61CF106691B502C9B92" />
<Co开发者_如何学GomponentRef Id="cmpAE88027B6D0222521E0ABA368319FF67" />
<ComponentRef Id="cmpF2490B3F3B6C3AFDCCE0B7D9E04B1307" />
<ComponentRef Id="cmpC3992B351478947A4EC528039072EE48" />
<ComponentRef Id="cmp70020D3E903A3C6460350DB8EDA762AE" />
<ComponentRef Id="cmp09907E3F88C8846C5036ED1B4E1E81DF" />
<ComponentRef Id="cmpB35A402A2163C959687B4BA4121F079A" />
<ComponentRef Id="cmp775993523FE108E37468C65AD0ACB4B4" />
<ComponentRef Id="cmp05F27393E5D1CAA313305B4861210845" />
<ComponentRef Id="cmp7FF316CEF60AD840CF18827C447F6127" />
<ComponentRef Id="cmpF6ECEDC852A8809386FCE1F6FD6BF773" />
<ComponentRef Id="cmp359949C0EE3700CD3FEDC58C2938CA02" />
<ComponentRef Id="cmpF07C3AB85632B07E2F0F35C8E102F750" />
<ComponentRef Id="cmp9BF2FC39D1074790EA6D5E031D4E9665" />
<ComponentRef Id="cmp781555C9539D6B197CC3E2862D633313" />
<ComponentRef Id="cmpA3348368AD2302BEA9380BFC071D019F" />
<ComponentRef Id="cmp976CCA18B7D5A52752D172D9F1B353E5" />
<ComponentRef Id="cmpD67BE195D2659B6309E067CEFCEBA782" />
<ComponentRef Id="cmpF8865B27ABD17AFD84B4C6DA99BA5982" />
<ComponentRef Id="cmp9DA6349D71DB330B293596A73268CCA4" />
<ComponentRef Id="cmpD6C59BB2981DADF2241DC165EDF05FD8" />
<ComponentRef Id="cmp0B8BBF1185264AB2CE511BAFC03E2F1B" />
</ComponentGroup>
</Fragment>
</Wix>
I would like to remove the Component Element and related ComponentRef that contains the Source attribute "SourceDir\Program.exe".
I therefore apply the following XSLT, based on other SO accepted answer: Removing related elements using XSLT 1.0
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="configComponent"
match="Component[File/@Source[substring(.,
string-length() - string-length('exe') + 1) = 'exe']]"
use="@Id" />
<xsl:template match="Component[key('configComponent', @Id)]" />
<xsl:template match="ComponentRef[key('configComponent', @Id)]" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The result is the same original XML with no elements removed.
I am using http://www.xmlper.com/ to test.
Any ideas as to why it is not working?
Regards
I think it's all about XML namespaces. Try the following:
- add an XML namespace for WiX elements:
xmlns:wix="http://schemas.microsoft.com/wix/2006/wix"
- prefix each WiX element in the XSLT stylesheet with
wix:
, like this:
<xsl:template match="wix:Component[key('configComponent', @Id)]" />
I don't think this is optimal or the "right" way to it, but it works for me :-)
<?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">
<!-- Copy all attributes and elements to the output. -->
<xsl:template match="@*|*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="*" />
</xsl:copy>
</xsl:template>
<xsl:output method="xml" indent="yes" />
<xsl:key name="exe-search"
match="wix:Component[contains(wix:File/@Source, 'Program.exe" use="@Id" />
<xsl:template match="wix:Component[key('exe-search', @Id)]" />
<xsl:template match="wix:ComponentRef[key('exe-search', @Id)]" />
</xsl:stylesheet>
精彩评论