【问题标题】:filter all elements with defined namespaces过滤所有定义了命名空间的元素
【发布时间】:2016-02-19 16:33:06
【问题描述】:

我有以下源 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-template:

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

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

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

【问题讨论】:

    标签: xml xslt namespaces xslt-2.0 xml-namespaces


    【解决方案1】:

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

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

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

    【讨论】:

    • 经过测试,这行得通。我的误解是关于选择 xpath-axe 而不是比较实际前缀。泰。
    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    相关资源
    最近更新 更多