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

发布于 2024-08-02 15:42:43 字数 445 浏览 11 评论 0原文

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

问题在于某些 Feed(例如此< /a>) 除了 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?

I would like to parse an Atom Feed and create an Atom-compliant cache of each Entry.

The problem is that some feeds (this one for example) have many namespaces other than the Atom one.

Is it possible to keep intact all Atom nodes and remove each node that belongs to another namespace?

Something like this:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2024-08-09 15:42:43

在 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 命名空间
  • 文本节点或注释

,也许您可​​以使用它。

In XSLT you could use this transformation:

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

This copies all nodes verbatim, if they are

  • in the default (empty) namespace
  • in the Atom namespace
  • text nodes or comments

Maybe you can use that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文