0
XPath.each( xmldoc, "//speech/speaking") do |element| 
# puts element.attributes['name']
# puts element.text
File.open(file_name + "_" + element.attributes['name'] + "-" + year + ".xml", 'a+') do |f|
    f.write("<speaker>" + element.attributes['name'] + "</speaker>")
    f.write("<speech>" + doc.xpath('//speech/speaking').text + "</speech>" + "\n")
end
end

Hello stackoverflow I am looking for help solving a logic issue I am having with XML files. The above code creates a file with the "speakers" name and then it should place what the speaker says into that file.

The problem that I am running into is that it places ALL of the speakers into the same file. So I am thinking the problem lies here:

f.write("<speech>" + doc.xpath('//speech/speaking').text + "</speech>" + "\n")

I am hoping that someone has a better way of doing this, but the idea would be to change the above code to:

doc.xpath('//speech/speaking').text WHERE speaker == element.attributes['name']

Ultimately I would like to have each speaker in their own XML file with their own speeches.

<speaking name="Mr. FAZIO">I appreciate my friend yielding.</speaking>

The above is a sample from the XML file.

CodeHard
  • 125
  • 14
  • 1
    isn't `element.text` enough? – Uri Agassi May 25 '14 at 07:04
  • Unfortunately not, the problem is the source XML file has hundreds of speakers with hundreds of speeches. The current code is creating the files properly but putting speeches from everyone in all the created XML files. – CodeHard May 25 '14 at 07:05

1 Answers1

0

The xpath you are looking for is:

doc.xpath("//speech/speaking[@name='#{element.attributes['name']}']").text

see XPath to select Element by attribute value

Community
  • 1
  • 1
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • This looks about right and I tried it before but it gives me an error: node.rb:159:in `evaluate': Invalid expression: //speech/speaking[@name=Mr. THOMAS] (Nokogiri::XML::XPath::SyntaxError) – CodeHard May 25 '14 at 07:09
  • Make sure you have enclosing quotes (`/speech/speaking[@name='Mr. THOMAS']`) – Uri Agassi May 25 '14 at 07:15
  • The above comment yields: speaking[@name=] (Nokogiri::XML::XPath::SyntaxError) – CodeHard May 25 '14 at 07:20
  • All I suggested was to make sure you enclosed the value with single quotes `'`... – Uri Agassi May 25 '14 at 07:22
  • It is enclosed. I feel that this is on the right track but I have been down this path and can not get it to work. I posted some sample XML above if you have any further ideas. – CodeHard May 25 '14 at 07:26
  • It took some guesswork (you are using both nokogiri AND rexml? why?), but I got your code to run with exactly my suggestion in the answer. – Uri Agassi May 25 '14 at 07:43
  • Sir, you are absolutely right. I made a mistake when I placed it into my file writing loop. I appreciate all your work and it was a pleasure to receive such prompt help. – CodeHard May 25 '14 at 07:50
  • To answer your query I am using both because I had to use REXML to rebuild the poorly formed XML files and tried to use nokogiri for the rest. – CodeHard May 25 '14 at 07:57