Ruby 如何获取不带';使用REXML不能有特定的祖先和后代
我想从以下XML中获取A标记和B标记,但我想删除第二个A标记:Ruby 如何获取不带';使用REXML不能有特定的祖先和后代,ruby,xpath,rexml,Ruby,Xpath,Rexml,我想从以下XML中获取A标记和B标记,但我想删除第二个A标记: ......many other tags. <A>abc</A> <A> <<==== I want to remove this A tag from result. <B>def <A>foo</A> <A>hoge</A> <A>bar</A> </B&
......many other tags.
<A>abc</A>
<A> <<==== I want to remove this A tag from result.
<B>def
<A>foo</A>
<A>hoge</A>
<A>bar</A>
</B>
</A>
.......
但是,此XPath两次获得B标记的内部A标记:
<A>abc</A>
<B>def
<A>foo</A>
<A>hoge</A>
<A>bar</A>
</B>
<A>foo</A>
<A>hoge</A>
<A>bar</A>
我想得到这个结果:
<A>abc</A>
<B>def
<A>foo</A>
<A>hoge</A>
<A>bar</A>
</B>
.......
abc
def
福
霍格
酒吧
.......
如何解决此问题?尝试使用下面的
XPath
表达式:
//*[self::A[not(./B) and not(./parent::B)] or self::B]
输出:
'<A>abc</A>'
'<B>def
<A>foo</A>
<A>hoge</A>
<A>bar</A>
</B>'
“abc”
“def
福
霍格
酒吧
'
self::A[not(./B)and not(./parent::B)]
表示没有直接子元素或父元素的A
B
元素尝试使用下面的XPath
表达式:
//*[self::A[not(./B) and not(./parent::B)] or self::B]
输出:
'<A>abc</A>'
'<B>def
<A>foo</A>
<A>hoge</A>
<A>bar</A>
</B>'
“abc”
“def
福
霍格
酒吧
'
self::A[非(./B)和非(./parent::B)]
表示没有直接子元素或父元素的A
,B
元素。虽然REXML附带Ruby,但Nokogiri速度更快,是事实上的标准。除了XPath之外,它还支持使用CSS选择器,允许您选择对特定查找更有效的选项。虽然REXML附带Ruby,但Nokogiri速度更快,是事实上的标准。除了XPath之外,它还支持使用CSS选择器,允许您选择对特定查找更有效的选项。