Roberto Pezzali
Roberto Pezzali

Reputation: 2494

Nokogiri, find XML node with multiple attributes and change text

I'm trying to change a indl file. The indl file is a file created by Adobe Indesign to keep the structure of a document, and is basically an XML. I want to use Nokogiri to find some selected XML nodes and replace the text with my text, saving then the xml to another file.

The XML of course is strange: i find some document to retrieve HTML tag with Nokogiri changing text but I don't know How I can manage a piece of XML like this:

<cflo>
<txsr prst="o_u5084" crst="o_u5085" trak="D_10">
    <pcnt>c_tEST</pcnt>
</txsr>
<txsr prst="o_u5086" crst="o_u5c" trak="D_20">
 <pcnt>c_Titolo titolo titolo</pcnt>
</txsr>
<cflo>

Basically I need to look for a combination of prst and crst attribute and replace the content inside the pcnt node.

I try with this

@doc.xpath("//txsr[prst='o_u5086' and crst='o_u5085']")

but I don't know how I can change ther text inside the pcnt node.

Upvotes: 0

Views: 1547

Answers (1)

kaineer
kaineer

Reputation: 56

  1. That's not the correct XPath. The correct XPath will look like this:

    @doc.xpath("//txsr[@prst='o_u5086'][@crst='o_u5085']")
    
  2. You should just take the first node from a set and use the inner_html= method to replace the text value.

Full code may be found here: https://gist.github.com/kaineer/7673698

Upvotes: 1

Related Questions