Gru*_*eck 68 xml xslt xslt-2.0 xslt-1.0
我有很多XML文件,它们具有以下形式:
<Element fruit="apple" animal="cat" />
Run Code Online (Sandbox Code Playgroud)
我想从文件中删除.
使用XSLT样式表和Linux命令行实用程序xsltproc,我该怎么做?
到目前为止,在脚本中我已经有了包含我想要删除的元素的文件列表,因此单个文件可以用作参数.
编辑:这个问题原本缺乏意图.
我想要实现的是删除整个元素"元素",其中(fruit =="apple"&& animal =="cat").在同一文件中有许多名为"元素"的元素,我希望这些元素保留下来.所以
<Element fruit="orange" animal="dog" />
<Element fruit="apple" animal="cat" />
<Element fruit="pear" animal="wild three eyed mongoose of kentucky" />
Run Code Online (Sandbox Code Playgroud)
会成为:
<Element fruit="orange" animal="dog" />
<Element fruit="pear" animal="wild three eyed mongoose of kentucky" />
Run Code Online (Sandbox Code Playgroud)
Dim*_*hev 131
使用最基本的XSLT设计模式之一:"覆盖身份转换 ",只需编写以下内容:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="Element[@fruit='apple' and @animal='cat']"/> </xsl:stylesheet>
请注意第二个模板如何仅为名为"Element"的元素覆盖identity(1st)模板,该元素具有值为"apple"的属性"fruit"和值为"cat"的属性"animal".此模板具有空体,这意味着匹配的元素将被简单地忽略(匹配时不会生成任何内容).
将此转换应用于以下源XML文档时:
<doc>... <Element name="same">foo</Element>... <Element fruit="apple" animal="cat" /> <Element fruit="pear" animal="cat" /> <Element name="same">baz</Element>... <Element name="same">foobar</Element>... </doc>
产生了想要的结果:
<doc>... <Element name="same">foo</Element>... <Element fruit="pear" animal="cat"/> <Element name="same">baz</Element>... <Element name="same">foobar</Element>... </doc>
可以在此处找到更多使用和覆盖标识模板的代码片段.
归档时间: |
|
查看次数: |
87631 次 |
最近记录: |