选择 xQuery 中的第 n 个子元素/选择下一个元素

发布于 2024-10-08 05:44:54 字数 1501 浏览 0 评论 0 原文

我很难找到好的 xQuery 教程,基本上我想做的是从这个 html 节点检索文本 etc...

<div class="venue">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">ADDRESS:</p>
        <p class="item-big">blabla</p>
    </div><br class="clear">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">PHONE:</p>
        <p class="item-big">123</p>
    </div><br class="clear">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">WEB:</p>
        <p class="item-big">etc...</p>
    </div><br class="clear">
</div>

我想知道如何从第三个​​中的第二个 p div[@class="vitem"]
或者直接跟在 p[@class="label"] 之后的 p包含文本 WEB:

<强>编辑:答案已经有很大帮助,但是我的第二个问题是如果布局更改为这样的方式

<div class="venue">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">ADDRESS:</p>
        <p class="item-big">blabla</p>
    </div><br class="clear">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">WEB:</p>
        <p class="item-big">etc...</p>
    </div><br class="clear">
</div>

我如何获得 etc...,只知道它遵循ap 的类标签包含文本 WEB:?它不再位于 div[3]/p[2] 中,

谢谢!

I am having trouble finding good xQuery tutorials, basically what I am trying to do is retreive the text etc... from this html node

<div class="venue">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">ADDRESS:</p>
        <p class="item-big">blabla</p>
    </div><br class="clear">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">PHONE:</p>
        <p class="item-big">123</p>
    </div><br class="clear">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">WEB:</p>
        <p class="item-big">etc...</p>
    </div><br class="clear">
</div>

I'd like to know how can I get the data out of the 2nd p in the third div[@class="vitem"]
Or the p directly following the p[@class="label"] that contains the text WEB:

Edit: Answeres already helped a great bit, however my second question is if the layout changes to something like this

<div class="venue">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">ADDRESS:</p>
        <p class="item-big">blabla</p>
    </div><br class="clear">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">WEB:</p>
        <p class="item-big">etc...</p>
    </div><br class="clear">
</div>

How do i get the etc..., knowing only that it follows a p with the class label containing the text WEB:? it's no longer in div[3]/p[2]

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-10-15 05:44:54

我想知道如何获得
第三个p中第二个p的数据
div[@class="vitem"]

使用

/*/div[@class='vitem'][3]/p[2]/text()

这意味着:获取所有divp子节点的所有文本节点子节点code> 元素,其属性 class 的值为 "vitem" 并且是顶部元素的子元素。

或者直接跟在 p 后面
p[@class="label"] 与数据 WEB:

使用

/*/div[@class='vitem'][3]/p[@class='label']
                             /following-sibling::p[1]/text()

这意味着:获取任何 p 的所有文本节点子节点>p 元素,其 class 属性值为 "label",它是所有具有 div 元素的第三个子元素属性 class ,其值为 "vitem" 并且是顶部元素的子元素。

更新:OP添加了第二个问题:他只想选择具有文本“etc...”作为字符串值的p

使用< /强>:

/*/div/p[.='etc...']

I'd like to know how can I get the
data out of the 2nd p in the third
div[@class="vitem"]

Use:

/*/div[@class='vitem'][3]/p[2]/text()

This means: get all the text node children of the second p child of the third of all div elements that have an attribute class with value "vitem" and that are children of the top element.

Or the p directly following the
p[@class="label"] with the data WEB:

Use:

/*/div[@class='vitem'][3]/p[@class='label']
                             /following-sibling::p[1]/text()

This means: get all the text node children of the first following-sibling p of any p element with class attribute with value "label", that is a child of the third of all div elements that have an attribute class with value "vitem" and that are children of the top element.

UPDATE: The OP has added a second question: he wants just to select the p that has as a string value the text "etc..."

Use:

/*/div/p[.='etc...']
入怼 2024-10-15 05:44:54

我想知道如何获得
第三个​​中第二个 p 的数据
div[@class="vitem"] 或 p直接跟随 p[@class="label"]

语义上相等的 XPath/ XQuery 表达式是:

/div
   /div[@class='vitem'][3]
      /p[2]

或者

/div
   /div[@class='vitem'][3]
      /p[@class='label']
         /following-sibling::p[1]

I'd like to know how can I get the
data out of the 2nd p in the third
div[@class="vitem"] Or the p directly following the p[@class="label"]

The semantically equal XPath/XQuery expressions are:

/div
   /div[@class='vitem'][3]
      /p[2]

Or

/div
   /div[@class='vitem'][3]
      /p[@class='label']
         /following-sibling::p[1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文