gpt4 book ai didi

xml - 使用 LibXML-Ruby 清除不需要的命名空间

转载 作者:行者123 更新时间:2023-12-04 16:55:20 26 4
gpt4 key购买 nike

我想解析一个 Atom 提要并为每个条目创建一个符合 Atom 的缓存。

问题是一些提要( this one for example )有许多命名空间而不是 Atom 命名空间。

是否可以保持所有 Atom 节点完整并删除属于另一个命名空间的每个节点?

像这样的东西:

valid_nodes = entry.find('atom:*', '/atom:feed/atom:entry')
# now I need to create an xml string with valid_nodes, but how I do that?

最佳答案

在 XSLT 中,您可以使用此转换:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2005/Atom"
>
<xsl:output method="xml" indent="yes" encoding="utf-8" />

<xsl:template match="node() | @*">
<xsl:if test="
namespace-uri() = ''
or
namespace-uri() = 'http://www.w3.org/2005/Atom'
">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:if>
</xsl:template>

<xsl:template match="text()|comment()">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>

这会逐字复制所有节点,如果它们是
  • 在默认(空)命名空间中
  • 在 Atom 命名空间中
  • 文本节点或评论

  • 也许你可以使用它。

    关于xml - 使用 LibXML-Ruby 清除不需要的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1341814/

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