【问题标题】:How to find node only in siblings using Xpath java?如何使用 Xpath java 仅在兄弟姐妹中查找节点?
【发布时间】:2018-01-13 21:14:17
【问题描述】:

我一直在寻找具有以下结构的 xml 的 Xpath....

<abc>
   <unique>
     <findme uniqueAtrr="itsme">Already Found Xpath For This</findme>
    </unique>
    <findme></findme>
    <findme></findme>
    <findme uniqueAtrr="itsme"> You Got Me</findme>   //NOTE : We don't know this position
    <findme></findme>   
</abc>  

我使用

获得了已经找到的 XPath
/abc//unique/findme[@uniqueAtrr="itsme"]

所以我认为找到的最佳方法是搜索在唯一标签中不存在的 findme...或仅在兄弟姐妹中找到节点...我尝试过的那个。 ..

/abc//findme

这会给我唯一标签内的那个:(

【问题讨论】:

    标签: java xml dom xpath nodes


    【解决方案1】:

    试试下面的 xpath 来匹配"You Got Me":

    /abc/unique/following-sibling::findme[@uniqueAtrr="itsme"]
    

    【讨论】:

    • 似乎它不起作用为什么我们需要 /unique ??
    • 你需要得到这个&lt;findme uniqueAtrr="itsme"&gt; You Got Me&lt;/findme&gt;,对吗?
    • 好的。那么为什么你认为它可能不起作用? &lt;unique&gt;&lt;findme uniqueAtrr="itsme"&gt; You Got Me&lt;/findme&gt; 的兄弟姐妹。它应该工作。试试看吧:)
    • WoooW 我让它工作了......我不能分享实际的 XML,所以它是虚拟的 xml 结构......根据你的逻辑,我得到了它的工作感谢你节省了我的一天 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多