0

I am using Nokogiri (1.5.9 - java) in JRuby ( 1.6.7.2 ) to copy an XML template and edit it. I'm having problems finding elements in the cloned document.

lblock = doc.xpath(".//lblock[@blockName='WINDOW_LIST']").first
lblock.children = new_children # kind of NodeSet or Node
copy_doc = doc.dup( 1 ) # or dup(0)
lblock = copy_doc.xpath(".//lblock[@blockName='WINDOW_LIST']").first # nil

When print to_s or to_xml, so lblock there is with new_children. Where is my mistake?

4

1 回答 1

0

我无法复制问题:

require 'nokogiri'

new_children = Nokogiri::XML::DocumentFragment.parse('<foo>bar</foo>')
doc = Nokogiri::XML(<<EOF)
<xml>
    <lblock blockName="WINDOW_LIST" />
</xml>
EOF

lblock = doc.xpath(".//lblock[@blockName='WINDOW_LIST']").first
lblock.children = new_children # kind of NodeSet or Node
copy_doc = doc.dup(1) # or dup(0)
lblock = copy_doc.xpath(".//lblock[@blockName='WINDOW_LIST']").first # nil

puts lblock.to_xml
puts
puts doc.to_xml

运行输出:

<lblock blockName="WINDOW_LIST">
  <foo>bar</foo>
</lblock>

<?xml version="1.0"?>
<xml>
  <lblock blockName="WINDOW_LIST"><foo>bar</foo></lblock>
</xml>

也就是说,这里的代码经过清理,向您展示了一些更简单的编写方法:

require 'nokogiri'

new_children = '<foo>bar</foo>'
doc = Nokogiri::XML(<<EOF)
<xml>
  <lblock blockName="WINDOW_LIST" />
</xml>
EOF

lblock = doc.at_xpath('//lblock')
lblock.children = new_children
copy_doc = doc.dup(1)
lblock = copy_doc.at_css('lblock')

puts lblock.to_xml
puts
puts doc.to_xml

运行后也会输出:

<lblock blockName="WINDOW_LIST">
  <foo>bar</foo>
</lblock>

<?xml version="1.0"?>
<xml>
  <lblock blockName="WINDOW_LIST"><foo>bar</foo></lblock>
</xml>

剖析代码:

lblock = doc.at_xpath('//lblock')
lblock = copy_doc.at_css('lblock')

它们使用两种不同的方式来寻找相同的东西。在本例中,因为示例 XML 很简单,所以我使用at了 ,它返回第一个匹配节点。at_xpathat_css分别使用 XPaths 和 CSS。at会尝试找出字符串是 CSS 还是 XPath,并且通常会正确,尽管我已经看到它被愚弄了。

lblock.children = new_children

在这种情况下,new_children是一个字符串。Nokogiri 足够聪明,知道它应该在使用之前将字符串转换为 XML 片段。这使得使用字符串修改 XML 或 HTML 文档变得非常容易,而不必创建 DocumentFragments。

于 2013-05-21T05:11:08.640 回答