基于复杂条件从XML中删除元素

基于复杂条件从XML中删除元素,xml,xslt,Xml,Xslt,我有一个如下所示的XML,需要使用XSLT1.0对其进行转换: <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Fragment> <DirectoryRef Id="Directory1"> <Component Id="Component1"> <File Id="File1" />

我有一个如下所示的XML,需要使用
XSLT1.0
对其进行转换:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="Directory1">
            <Component Id="Component1">
                <File Id="File1" />
            </Component>
            <Component Id="Component2">
                <File Id="File2"/>
            </Component>
        </DirectoryRef>
        <DirectoryRef Id="Directory2">
            <Component Id="Component3">
                <File Id="File3" />
            </Component>
            <Component Id="Component4">
                <File Id="File4"/>
            </Component>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="Group">
            <ComponentRef Id="Component1" />
            <ComponentRef Id="Component2" />
            <ComponentRef Id="Component3" />
            <ComponentRef Id="Component4" />
        </ComponentGroup>
    </Fragment>
</Wix>

我需要删除Id为Directory1的元素和他的所有孩子,我已经完成了。但是我还需要删除Id与我已删除的
元素(Directory1的子元素)匹配的所有
元素

因此,在这种情况下,期望输出为:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="Directory2">
            <Component Id="Component3">
                <File Id="File3" />
            </Component>
            <Component Id="Component4">
                <File Id="File4"/>
            </Component>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="Group">
            <ComponentRef Id="Component3" />
            <ComponentRef Id="Component4" />
        </ComponentGroup>
    </Fragment>
</Wix>

我已经在迭代
元素并删除它们,我需要一种方法来使用每个Id匹配
元素并删除它们

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wi="http://schemas.microsoft.com/wix/2006/wi">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="*[wi:Component/parent::node()[@Name='Directory1']]"/>

</xsl:stylesheet>

尝试使用



工作小提琴:

谢谢你的回答!在实际实现中,我必须将
wi:DirectoryRef[@Id='Directory1']]/wi:Component
更改为
wi:DirectoryRef[@Id='Directory1']/*
,以匹配层次结构中较深的
元素。@fsinisi90-只需将键中的匹配更改为
wi:DirectoryRef[@Id='Directory1']///wi:Component
(注意
wi:Component
之前的
/
)@fsinisi90-啊,你已经知道了。:-)这里有一个更新的提琴,以防万一:是的,我在发送评论后才知道。再次感谢!另外,如果没有
xsl:key
,您可以使用