XSL Transform removes Xml elements

I'm at a dead end. Given an XML document 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 Source = "My.Exe" is in. In this case, delete the "Com" element, where its attribute id = BEED24F05AB78FB588F61D4092654B6D.

I have done it. But what I cannot do also removes the "CompRef" element, where Id = BEED24F05AB78FB588F61D4092654B6D.

So, after the conversion, I want my xml to look like this:

    <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 are a few xml that remove the "FileName" element.

  <xsl:template match="Com/FileName[contains(@Source,'My.Exe')='true']">
  </xsl:template>

So the result is:

<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 xsl: apply-template does nothing, since it is stuck in the node of its operation. I do not know how to store the identifiers that I want to delete, and then scroll through them.

2

node, "Com", source = "MyExe". , Id , -.

+3
2

:

<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>

XML- (, ):

<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>

:

<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>
+5

, , selector :   

<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>
-1

Source: https://habr.com/ru/post/1752489/


All Articles