ruby - 如何使用 ruby​​、xpath、rexml 从子上下文获取节点文本

标签 ruby xml xpath rexml

我在获取 REXML::XPath.first 从子上下文中呈现正确的节点文本时遇到问题。

请参阅下面的测试脚本和 xml。

测试.rb

require 'rexml/document'
require 'rexml/xpath'

file = File.new('test.xml')
doc = REXML::Document.new(file)

employers = REXML::XPath.match(doc, '//EmployerOrg')
employers.each do |employer|
  # this looks fine, position_history is being set for each employer
  position_history = REXML::XPath.first(employer, 'PositionHistory')

  # always returns the title from the first employer, in spite of the position_history context
  p title = REXML::XPath.first(position_history, '//Title').text
end

输出:

"Director of Web Applications Development"
"Director of Web Applications Development"
"Director of Web Applications Development"

示例 XML:

<?xml version="1.0" encoding="UTF-8"?>
<Resume xml:lang="en" xmlns="http://ns.hr-xml.org/2006-02-28" xmlns:sov="http://sovren.com/hr-xml/2006-02-28">
  <StructuredXMLResume>
    <EmploymentHistory>
      <EmployerOrg>
        <EmployerOrgName>Technical Difference</EmployerOrgName>
        <PositionHistory positionType="directHire" currentEmployer="true">
          <Title>Director of Web Applications Development</Title>
          <OrgName>
            <OrganizationName>Technical Difference</OrganizationName>
          </OrgName>
          <StartDate>
            <AnyDate>2004-10-01</AnyDate>
          </StartDate>
          <EndDate>
            <AnyDate>2015-09-15</AnyDate>
          </EndDate>
        </PositionHistory>
      </EmployerOrg>
      <EmployerOrg>
        <EmployerOrgName>Convergence Inc. LLC</EmployerOrgName>
        <PositionHistory positionType="directHire">
          <Title>Senior Web Developer/DBA</Title>
          <OrgName>
            <OrganizationName>Convergence Inc. LLC</OrganizationName>
          </OrgName>
          <StartDate>
            <AnyDate>2003-03-01</AnyDate>
          </StartDate>
          <EndDate>
            <AnyDate>2004-12-01</AnyDate>
          </EndDate>
          <UserArea>
            <sov:PositionHistoryUserArea>
              <sov:Id>POS-2</sov:Id>
              <sov:CompanyNameProbability>23</sov:CompanyNameProbability>
              <sov:PositionTitleProbability>30</sov:PositionTitleProbability>
            </sov:PositionHistoryUserArea>
          </UserArea>
        </PositionHistory>
      </EmployerOrg>
      <EmployerOrg>
        <EmployerOrgName>Avalon Digital Marketing Systems, Inc</EmployerOrgName>
        <PositionHistory positionType="contract">
          <Title>Contractor - Web Development</Title>
          <OrgName>
            <OrganizationName>Avalon Digital Marketing Systems, Inc</OrganizationName>
          </OrgName>
          <StartDate>
            <AnyDate>2002-05-01</AnyDate>
          </StartDate>
          <EndDate>
            <AnyDate>2003-03-01</AnyDate>
          </EndDate>
        </PositionHistory>
        <PositionHistory positionType="directHire">
          <Title>Web Developer/Junior DBA</Title>
          <OrgName>
            <OrganizationName>European Division</OrganizationName>
          </OrgName>
          <StartDate>
            <AnyDate>2000-05-01</AnyDate>
          </StartDate>
          <EndDate>
            <AnyDate>2002-04-30</AnyDate>
          </EndDate>
        </PositionHistory>
      </EmployerOrg>
    </EmploymentHistory>
  </StructuredXMLResume>
</Resume>

最佳答案

可能是因为您的 XPath '//Title' 表示从文档顶部开始,几乎忽略了上下文节点 position_history。尝试将其替换为 './Title' 或仅 'Title'

关于ruby - 如何使用 ruby​​、xpath、rexml 从子上下文获取节点文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32639914/

相关文章:

Ruby 递归不起作用?

ruby-on-rails - Rails 3.2:尝试使用 'Whenever' gem 但日志文件不断给出:无法在任何来源中找到 multi_json-1.1.0 (Bundler::GemNotFound) 错误

python - 使用 Python/ElementTree 为 XML 中的元素插入节点

Javascript XML 解析或替代方案

c# - Xml 文档未打包在类库中

arrays - Ruby 数组中的 `return`#map

javascript - SoundCloud API 可以直接从 Windows 访问(例如使用 python 脚本)还是必须是从服务器运行的互联网应用程序?

python selenium 找不到 iframe xpath

php - xpath查找非空链接

php - 如何在PHP中使用XPath查询DOMNode?