gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:02:40 26 4
gpt4 key购买 nike

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

这给出了空输出。我需要在 <xsl:if...></xsl:if> 中找出条件陈述。请帮我:)

更新:
由于使用了 Saxon-HE 9.5.1.7,xslt 2.0 解决方案没问题。

最佳答案

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

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

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

关于xml - 过滤所有具有定义命名空间的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35510187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com