XSL 转换删除 Xml 元素
我很困惑。给定一个 xml 文档,例如:
<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。
我已经这么做了。但我不能做的就是删除 Id=BEED24F05AB78FB588F61D4092654B6D 的“CompRef”元素。
因此,在转换之后,我希望我的 xml 看起来像:
<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。
<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,然后循环它们。
更新2
可以删除多个节点,即多个“Com”元素,其中source="MyExe"。此外,ID 是自动生成的,因此每次运行都会有所不同。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此转换:
当应用于提供的 XML 文档时(经过更正以使其格式正确):
产生所需的正确输出:
This transformation:
when applied on the provided XML document (corrected to be made well-formed):
produces the wanted, correct output:
这确实很快且未经测试,但您希望选择器属性不等于某个值:
This is REALLY quick and untested but you want the selector attribute to not be equal to a value: