[英]Using Nokogiri How to Change Text Value of Node using Nokogiri::XML::Text wrong number of arguments
[英]Nokogiri, find XML node with multiple attributes and change text
我正在嘗試更改indl文件。 indl文件是Adobe Indesign創建的用於保留文檔結構的文件,基本上是XML。 我想使用Nokogiri查找一些選定的XML節點,然后用我的文本替換文本,然后將xml保存到另一個文件。
XML當然很奇怪:我找到了一些文檔來用Nokogiri更改文本來檢索HTML標簽,但是我不知道如何管理這樣的XML:
<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>
基本上,我需要查找prst
和crst
屬性的組合,並替換pcnt
節點內的內容。
我嘗試這個
@doc.xpath("//txsr[prst='o_u5086' and crst='o_u5085']")
但是我不知道如何更改pcnt
節點內的文本。
那不是正確的XPath。 正確的XPath將如下所示:
@doc.xpath("//txsr[@prst='o_u5086'][@crst='o_u5085']")
您應該只從集合中取出第一個節點,然后使用inner_html=
方法替換文本值。
完整的代碼可以在這里找到: https : //gist.github.com/kaineer/7673698
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.