【发布时间】:2023-03-30 14:59:01
【问题描述】:
当我找到匹配的子节点时,我想从 XML 源返回一个“父”节点。
如果我搜索“ddd”:
@doc.xpath('//item[contains(., "ddd")]')
我想返回“第 2 节”
我找不到关于 Nokogiri 的“where”类型代码的文档。 这甚至可能吗??
<entry>
<match>
<field>Section 1</field>
<child>
<item>aaa</item>
<item>bbb</item>
<item>ccc</item>
</child>
</match>
<match>
<field>Section 2</field>
<child>
<item>ddd</item>
<item>eee</item>
<item>fff</item>
</child>
</match>
<match>
<field>Section 3</field>
<child>
<item>hhh</item>
<item>iii</item>
<item>jjj</item>
</child>
</match>
</entry>
这是一种更准确的 XML 格式。 我需要在节点树上上下移动才能获得第 2 节。 这可能吗?
<entry>
<match>
<field>
<foo>Section 1</foo>
</field>
<child>
<item>aaa</item>
<item>bbb</item>
<item>ccc</item>
</child>
</match>
<match>
<field>
<foo>Section 2</foo>
</field>
<child>
<item>ddd</item>
<item>eee</item>
<item>fff</item>
</child>
</match>
<match>
<field>
<foo>Section 3</foo>
</field>
<child>
<item>hhh</item>
<item>iii</item>
<item>jjj</item>
</child>
</match>
</entry>
【问题讨论】: