如何使用带有样式表和 xsltproc 的 xslt 从 xml 中删除元素? [英] How to remove elements from xml using xslt with stylesheet and xsltproc?

查看:27
本文介绍了如何使用带有样式表和 xsltproc 的 xslt 从 xml 中删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多具有以下形式的 XML 文件:

我想从文件中删除的内容.

使用 XSLT 样式表和 Linux 命令行实用程序 xsltproc,我该怎么做?

到此脚本中,我已经有了包含要删除的元素的文件列表,因此可以将单个文件用作参数.


这个问题最初缺乏意图.

我想要实现的是删除整个元素元素",其中(水果==苹果"&&动物==猫").在同一个文档中有许多名为元素"的元素,我希望保留这些元素.所以

<元素水果=苹果"动物=猫"/><Elementfruit="pear"animal="肯塔基州野生三眼猫鼬"/>

会变成:

<Elementfruit="pear"animal="肯塔基州野生三眼猫鼬"/>

解决方案

使用最基本的 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:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><xsl:template match="Element[@fruit='apple' and @animal='cat']"/></xsl:样式表>

请注意第二个模板如何仅针对名为Element"的元素覆盖标识(第一个)模板,这些元素的属性fruit"值为apple",属性animal"值为猫".这个模板的主体是空的,这意味着匹配的元素被简单地忽略(匹配时不产生任何东西).

当此转换应用于以下源 XML 文档时:

<前><文档>...<Element name="same">foo</Element>...<元素水果="苹果"动物="猫"/><元素水果="梨"动物="猫"/><Element name="same">baz</Element>...<Element name="same">foobar</Element>...

产生了想要的结果:

<前><文档>...<Element name="same">foo</Element>...<元素水果="梨"动物="猫"/><Element name="same">baz</Element>...<Element name="same">foobar</Element>...

可以找到更多使用和覆盖身份模板的代码片段在这里.

I have a lot of XML files which have something of the form:

<Element fruit="apple" animal="cat" />

Which I want to be removed from the file.

Using an XSLT stylesheet and the Linux command-line utility xsltproc, how could I do this?

By this point in the script I already have the list of files containing the element I wish to remove, so the single file can be used as a parameter.


EDIT: the question was originally lacking in intention.

What I am trying to achieve is to remove the entire element "Element" where (fruit=="apple" && animal=="cat"). In the same document there are many elements named "Element", I wish for these to remain. So

<Element fruit="orange" animal="dog" />
<Element fruit="apple"  animal="cat" />
<Element fruit="pear"   animal="wild three eyed mongoose of kentucky" />

Would become:

<Element fruit="orange" animal="dog" />
<Element fruit="pear"   animal="wild three eyed mongoose of kentucky" />

解决方案

Using one of the most fundamental XSLT design patterns: "Overriding the identity transformation" one will just write the following:

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

Do note how the second template overrides the identity (1st) template only for elements named "Element" that have an attribute "fruit" with value "apple" and attribute "animal" with value "cat". This template has empty body, which means that the matched element is simply ignored (nothing is produced when it is matched).

When this transformation is applied on the following source XML document:

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

the wanted result is produced:

<doc>... 
    <Element name="same">foo</Element>...
    <Element fruit="pear" animal="cat"/>
    <Element name="same">baz</Element>...
    <Element name="same">foobar</Element>...
</doc>

More code snippets of using and overriding the identity template can be found here.

这篇关于如何使用带有样式表和 xsltproc 的 xslt 从 xml 中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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