Reputation: 8869
I am stumped. Given an xml doc like:
<Frag>
<DirRef Id="BeemzDir">
<Com Id="BEED24F05AB78FB588F61D4092654B6D" Guid="{A11AB356-2F45-4157-92EF-ED49F5BE0F70}">
<FileName Id="fil1" KeyPath="yes" Source="My.Exe" />
</Com>
<Com Id="FFF24F05AB78FB588F61D4092654CCC" Guid="{A11AB356-2F45-4157-92EF-ED49F5BE0F70}">
<FileName Id="fil2" KeyPath="yes" Source="My.Dll" />
</Com>
</DirRef>
</Frag>
<Frag>
<ComGroup Id="MyGroup">
<CompRef Id="BEED24F05AB78FB588F61D4092654B6D" />
<CompRef Id="FFF24F05AB78FB588F61D4092654CCC" />
</ComGroup>
</Frag>
I need to use xslt to remove the element that houses the Source="My.Exe". In this case remove element "Com" where its attribute id=BEED24F05AB78FB588F61D4092654B6D.
I have done that. But what I cant do is also remove the "CompRef" element where Id=BEED24F05AB78FB588F61D4092654B6D.
So after transformation I want my xml to look like:
<Frag>
<DirRef Id="BeemzDir">
<Com Id="FFF24F05AB78FB588F61D4092654CCC" Guid="{A11AB356-2F45-4157-92EF-ED49F5BE0F70}">
<FileName Id="fil2" KeyPath="yes" Source="My.Dll" />
</Com>
</DirRef>
</Frag>
<Frag>
<ComGroup Id="MyGroup">
<CompRef Id="FFF24F05AB78FB588F61D4092654CCC" />
</ComGroup>
</Frag>
Any help would be appreciated.
Update
Here is some xml that deletes the "FileName" element.
<xsl:template match="Com/FileName[contains(@Source,'My.Exe')='true']">
</xsl:template>
So that results in:
<Frag>
<DirRef Id="BeemzDir">
<Com Id="BEED24F05AB78FB588F61D4092654B6D" Guid="{A11AB356-2F45-4157-92EF-ED49F5BE0F70}">
</Com>
<Com Id="FFF24F05AB78FB588F61D4092654CCC" Guid="{A11AB356-2F45-4157-92EF-ED49F5BE0F70}">
<FileName Id="fil2" KeyPath="yes" Source="My.Dll" />
</Com>
</DirRef>
</Frag>
<Frag>
<ComGroup Id="MyGroup">
<CompRef Id="BEED24F05AB78FB588F61D4092654B6D" />
<CompRef Id="FFF24F05AB78FB588F61D4092654CCC" />
</ComGroup>
</Frag>
Changing the above xsl that calls an xsl:apply-template doesn nothing, as its stuck in the node its operating in. I dont know how to store the Ids I want to delete and then loop through them.
Update 2
There can be more than one node to delete, that is multiple "Com" elements where source="MyExe". Also the Id is autogenerated so will be different each this is run.
Upvotes: 3
Views: 6107
Reputation: 243599
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Com[FileName/@Source='My.Exe']"/>
<xsl:template match="CompRef[@Id=/*/*/*/Com[FileName/@Source='My.Exe']/@Id]"/>
</xsl:stylesheet>
when applied on the provided XML document (corrected to be made well-formed):
<Frags>
<Frag>
<DirRef Id="BeemzDir">
<Com Id="BEED24F05AB78FB588F61D4092654B6D" Guid="{A11AB356-2F45-4157-92EF-ED49F5BE0F70}">
<FileName Id="fil1" KeyPath="yes" Source="My.Exe" />
</Com>
<Com Id="FFF24F05AB78FB588F61D4092654CCC" Guid="{A11AB356-2F45-4157-92EF-ED49F5BE0F70}">
<FileName Id="fil2" KeyPath="yes" Source="My.Dll" />
</Com>
</DirRef>
</Frag>
<Frag>
<ComGroup Id="MyGroup">
<CompRef Id="BEED24F05AB78FB588F61D4092654B6D" />
<CompRef Id="FFF24F05AB78FB588F61D4092654CCC" />
</ComGroup>
</Frag>
</Frags>
produces the wanted, correct output:
<Frags>
<Frag>
<DirRef Id="BeemzDir">
<Com Id="FFF24F05AB78FB588F61D4092654CCC" Guid="{A11AB356-2F45-4157-92EF-ED49F5BE0F70}">
<FileName Id="fil2" KeyPath="yes" Source="My.Dll"/>
</Com>
</DirRef>
</Frag>
<Frag>
<ComGroup Id="MyGroup">
<CompRef Id="FFF24F05AB78FB588F61D4092654CCC"/>
</ComGroup>
</Frag>
</Frags>
Upvotes: 5
Reputation: 34227
This is REALLY quick and untested but you want the selector attribute to not be equal to a value:
<xsl:template match="node()[!@Id='BEED24F05AB78FB588F61D4092654B6D']">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
<xsl:template match="frag"/>
<xsl:template match="@*|node()|processing-instruction()|comment()">
<xsl:copy>
<xsl:apply-templates
select="@*|node()|processing-instruction()|comment()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: -1