簡體   English   中英

如何使用REXML獲取沒有特定祖先和后代的標記

[英]How to get tags that don't have specific ancestor and descendant using 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>
 </A>
 .......

我正在使用這個XPath:

//*[self::A[not(descendant::B) or self::B]]

但是,此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>

然后,我寫了這個Xpath,但它不起作用:

//*[self::A[not(descendant::B or ancestor::B) or self::B]]

我想得到這個結果:

 <A>abc</A>
   <B>def
      <A>foo</A>
      <A>hoge</A>
      <A>bar</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>'

self::A[not(./B) and not(./parent::B)]是指A不具有直接子或父B元件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM