Removing elements using XSLT 1.0
- by pmdarrow
I'm attempting to remove Component elements from the XML below that have File children with the extension "config." I've managed to do this part, but I also need to remove the matching ComponentRef elements that have the same IDs as these Components.
<Fragment>
<DirectoryRef Id="MyWebsite">
<Component Id="Comp1">
<File Source="Web.config" />
</Component>
<Component Id="Comp2">
<File Source="Default.aspx" />
</Component>
</DirectoryRef>
</Fragment>
<Fragment>
<ComponentGroup Id="MyWebsite">
<ComponentRef Id="Comp1" />
<ComponentRef Id="Comp2" />
</ComponentGroup>
</Fragment>
Based on other SO answers, I've come up with the following XSLT to remove these Component elements:
<?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:template match="Component[File[substring(@Source, string-length(@Source)- string-length('config') + 1) = 'config']]" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Unfortunately, this doesn't remove the matching ComponentRef elements. The XSLT will remove the component with the Id "Comp1" but not the ComponentRef with Id "Comp1". How do I achieve this using XSLT 1.0?