使用 Nokogiri 获取具有命名空间的属性值 [英] Get the values of attributes with namespace, using Nokogiri

查看:39
本文介绍了使用 Nokogiri 获取具有命名空间的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从 .docx 文件中提取的 Nokogiri 解析 document.xml 文件,需要获取带有名称的属性值,例如w:val".

I'm parsing a document.xml file using Nokogiri, extracted from .docx file and need to get values of attributes with names, like "w:val".

这是源 XML 的示例:

This is a sample of the source XML:

<w:document>
  <w:body>
    <w:p w:rsidR="004D5F21" w:rsidRPr="00820E0B" w:rsidRDefault="00301D39" pcut:cut="true">
      <w:pPr>
        <w:jc w:val="center"/>
      </w:pPr>
  </w:body>
</w:document>

这是代码示例:

require 'nokogiri' 

doc = Nokogiri::XML(File.open(path))
  doc.search('//w:jc').each do |n|
    puts n['//w:val']
  end

控制台里什么都没有,只有空行.如何获取属性的值?

There is nothing in the console, only empty lines. How can I get the values of the attributes?

推荐答案

require 'nokogiri' 

doc = Nokogiri::XML(File.open(path))
  doc.xpath('//jc').each do |n|
    puts n.attr('val')
  end

应该可以.不要忘记查看文档:http://nokogiri.org/tutorials/searching_a_xml_html_document.html#fn:1

Should work. Don't forget to look at the docs : http://nokogiri.org/tutorials/searching_a_xml_html_document.html#fn:1

这篇关于使用 Nokogiri 获取具有命名空间的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆