过滤具有已定义命名空间的所有元素

filter all elements with defined namespaces

我有以下源 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:filter="filter_this" xmlns:also_filter="filter_also">
    <filter:error>This element should be filtred</filter:error>
    <ok>This is ok</ok>
    <filter:error>This element should be filtred also</filter:error>
    <ok>This is also ok</ok>
</root>

任务是过滤带有命名空间的元素,比如*:*root 元素中可能存在具有随机命名空间的元素,而不仅仅是我仅作为示例给出的 filteralso_filter

因此所需的输出应如下所示:

This is ok
This is also ok

我尝试了以下 xslt 模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output indent="yes" method="text"/>

    <xsl:template match="/">
        <xsl:for-each select="/root/*">
            <xsl:if test="not(namespace::*)">
                <xsl:copy-of select="."/>
                <xsl:text>&#xa;</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

 </xsl:stylesheet>

给出空输出。我需要弄清楚 <xsl:if...></xsl:if> 语句中的条件。请帮助我:)

更新: 由于使用 Saxon-HE 9.5.1.7,xslt 2.0 解决方案还可以。

您当前的解决方案失败,因为根目录下所有元素的命名空间轴包含两个节点(对于命名空间 filter_thisfilter_also)。所以 if 总是计算为 false。

但是你可以测试一个元素是否没有命名空间uri:

<xsl:if test="not(namespace-uri())">