简体   繁体   English

使用 XPath,如何在标签后精确定位文本?

[英]Using XPath, how can I exact text following a tag?

I want to extract Text2 which without a tag wrapped.我想提取没有包装标签的Text2

It's next to div.header-nav , I'm not sure it's the second text.它在div.header-nav旁边,我不确定它是第二个文本。

<body>
  Text1
    <div class="header-nav">
        nav text
    </div>
  Text2
    <div>Something</div>
  Text3
</body>

If you know that Text2 is the second text node directly under <body> , this should do it:如果您知道Text2<body>正下方的第二个文本节点,则应该这样做:

//text()[parent::body][2]

Edit:编辑:

If you don't know the position but you know the node it immediately follows, try this:如果您不知道位置,但您知道它紧随其后的节点,请尝试以下操作:

//text()[parent::body][preceding-sibling::div[1][@class="header-nav"]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM