如何从样式表和xsltproc使用xslt从xml中移除元素?
我有很多的XML文件有一些forms的东西:
<Element fruit="apple" animal="cat" /> 我想从文件中删除。
使用XSLT样式表和Linux命令行实用程序xsltproc,我该怎么做?
通过脚本中的这一点,我已经有了包含我希望删除的元素的文件列表,所以单个文件可以用作参数。
编辑:这个问题原本是缺乏意图。
我试图实现的是去除整个元素“元素”,其中(水果==“苹果”&&动物==“猫”)。 在同一个文档中有很多元素叫“元素”,我希望这些元素保持不变。 所以
 <Element fruit="orange" animal="dog" /> <Element fruit="apple" animal="cat" /> <Element fruit="pear" animal="wild three eyed mongoose of kentucky" /> 
会成为:
 <Element fruit="orange" animal="dog" /> <Element fruit="pear" animal="wild three eyed mongoose of kentucky" /> 
	
使用最基本的XSLTdevise模式之一:“重写身份转换 ”,只会写下如下内容:
 <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:拷贝>
          <xsl:apply-templates select =“node()| @ *”/>
       </ XSL:复制>
     </ XSL:模板>
     <xsl:template match =“元素[@ fruit ='apple'and @ animal ='cat']”/>
 </ XSL:样式>
请注意第二个模板如何replace名为“Element”的元素的identity(1st)模板,该元素的属性为“fruit”,值为“apple”,属性“animal”的值为“cat”。 这个模板有空的主体,这意味着匹配的元素被忽略(匹配时不会产生任何结果)。
将此转换应用于以下源XML文档时:
 <文档> ... 
     <Element name =“same”> foo </ Element> ...
     <元素水果=“苹果”动物=“猫”/>
     <元素水果=“梨”动物=“猫”/>
     <Element name =“same”> baz </ Element> ...
     <Element name =“same”> foobar </ Element> ...
 </ DOC>
想要的结果是:
 <文档> ... 
     <Element name =“same”> foo </ Element> ...
     <元素水果=“梨”动物=“猫”/>
     <Element name =“same”> baz </ Element> ...
     <Element name =“same”> foobar </ Element> ...
 </ DOC>
更多使用和覆盖身份模板的代码片段可以在这里find。