XSL 转换删除 Xml 元素 [英] XSL Transform remove Xml Elements

查看:38
本文介绍了XSL 转换删除 Xml 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被难住了.给定一个 xml 文档,如:

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> 

我需要使用 xslt 删除包含 Source="My.Exe" 的元素.在这种情况下,删除元素Com",其属性 id=BEED24F05AB78FB588F61D4092654B6D.

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.

我已经做到了.但我不能做的是删除CompRef"元素,其中 Id=BEED24F05AB78FB588F61D4092654B6D.

I have done that. But what I cant do is also remove the "CompRef" element where Id=BEED24F05AB78FB588F61D4092654B6D.

所以转换后我希望我的 xml 看起来像:

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> 

任何帮助将不胜感激.

更新

这是一些删除FileName"元素的 xml.

Here is some xml that deletes the "FileName" element.

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

结果是:

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

更改上面调用 xsl:apply-template 的 xsl 没有任何作用,因为它卡在其运行的节点中.我不知道如何存储我想删除的 Id,然后循环遍历它们.

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.

更新 2

可以删除多个节点,即多个Com"元素,其中 source="MyExe".此外,Id 是自动生成的,因此每次运行都会有所不同.

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.

推荐答案

这种转变:

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

这篇关于XSL 转换删除 Xml 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆