1

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

问题是一些提要(例如这个)除了 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?
4

1 回答 1

2

在 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 命名空间中
  • 文本节点或评论

也许你可以使用它。

于 2009-08-27T16:26:08.110 回答