How to get node text from child context using ruby, xpath, rexml

doremi

I'm having an issue getting REXML::XPath.first to render the correct node text from a child context.

See the test script and xml below.

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

Output:

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

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

Probably because your XPath '//Title' is saying to start at the top of the document, pretty much ignoring the context-node position_history. Try replacing that with './Title' or just 'Title'.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

XPath given node text match get text from child node

How to get the text of a node before its first child node by xpath?

how to use xpath get all child node text?

Get text from a grand-child using XPath and contains function

XPath: Get parent node from child node

How to get child node using context.expand in groovy?

How to get text from element using xpath

Get text from XPath of immediate child

Extract all Text from child elements/node using xpath text() function

How to get text that has no node and attribute using xpath in selenium

XPath: How to select child node using value of second child node

How to reach specific child node using XPATH?

Select the entire text from the following node with child nodes using xpath query in python

How to get the text using XPATH

xpath: how to get the specific text using xpath?

Cannot get the text out of node by using XPath

How to extract the text from the parent node excluding the text from the child node using Selenium

How get the text from the <p> tag using XPath Selenium and Python

How to get the parent node from child using XSLT

Xpath: How to combine these two child node to get parent node?

How to get the node with certain text value in xpath?

ag-grid - how to get parent node from child grid via context menu?

Using Ruby and watir-webdriver, how does one get the XPath of an element if the text is known?

Xpath for node without text but child has text

XPath of text from child element

how to get the text of these paragraphs using selenium and xpath

How to select a sibling's child node using XPath in Python?

xPath: How to get 'title' text from table?

How to select child node with XPath