Warm tip: This article is reproduced from serverfault.com, please click

其他-如何使用XSLT基于值删除特定的XML元素

(其他 - How to remove specific XML elements based on value with XSLT)

发布于 2020-12-02 17:00:05

我认为我要尝试的事情很简单,但是由于我对XSLT并不熟悉,并且已经尝试/搜索了大约4个小时,所以我想问一下...

假设我们有要转换的XML:

<?xml version="1.0" encoding="UTF-8"?>
<node>
    <field1>test</field1>
    <field2>/</field2>
    <field3>test2</field3>
    <field4>/<field4>
</node>

我想使用XSLT 1.0对其进行转换,以便复制整个结构和所有值,值“ /”的元素除外应该很容易吧?到目前为止,我已经发现了这一点,我认为这与我的需求相距不远,但尚未奏效……

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

 <xsl:template match="//text()='/'" />

</xsl:transform>

所以我期望的输出是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<node>
    <field1>test</field1>
    <field3>test2</field3>
</node>

尝试此操作时,出现未指定的错误。未指定,因为我正在使用免费的在线工具进行转换。任何帮助将不胜感激!

谢谢!

问候,尼克

Questioner
nikiforos6
Viewed
0
michael.hor257k 2020-12-03 01:20:31

//text()='/' 不是有效的位置路径。

无论如何,你都想删除元素,而不是文本节点-因此你应该使用类似以下内容的东西:

<xsl:template match="*[.='/']" />