代码之家  ›  专栏  ›  技术社区  ›  collimarco

使用libxml ruby清除不需要的命名空间

  •  1
  • collimarco  · 技术社区  · 17 年前

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

    问题是有些饲料( this one for example )有许多名称空间,原子名称空间除外。

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

    像这样:

    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?
    1 回复  |  直到 17 年前
        1
  •  2
  •   Tomalak    17 年前

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

    也许你可以用那个。

    推荐文章