我有一个 HTML 文档,如下所示:
<root><template>title</template>
<h level="3" i="3">Something</h>
<template element="1"><title>test</title></template>
# one
# two
# three
# four
<h level="4" i="5">something1</h>
some random test
<template element="1"><title>test</title></template>
# first
# second
# third
# fourth
<template element="2"><title>testing</title></template>
我想提取:
# one
# two
# three
# four
# first
# second
# third
# fourth
</root>
换句话说,我想要“之后的所有文本<template element="1"><title>test</title></template>
以及在那之后开始的下一个标签之前。”
我可以使用 root 之间的所有文本'//root/text()'
但如何获取某些标签之前和之后的所有文本?
这似乎有效:
require 'nokogiri'
xml = '<root>
<template>title</template>
<h level="3" i="3">Something</h>
<template element="1">
<title>test</title>
</template>
# one
# two
# three
# four
<h level="4" i="5">something1</h>
some random test
<template element="1">
<title>test</title>
</template>
# first
# second
# third
# fourth
<template element="2">
<title>testing</title>
</template>
</root>
'
doc = Nokogiri::XML(xml)
text = (doc / 'template[@element="1"]').map{ |n| n.next_sibling.text.strip.gsub(/\n +/, "\n") }
puts text
# >> # one
# >> # two
# >> # three
# >> # four
# >> # first
# >> # second
# >> # third
# >> # fourth
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)