如何获取所有同级 ul 标签?

发布于 2025-01-05 16:57:32 字数 904 浏览 1 评论 0原文

这是一个 HTML 片段: 来自:http://www.sphere-light.com/compatibility/maker/ id/4/

<h3 class="my-subject">a</h3>

<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>

<h3 class="my-subject">b</h3>

<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>

如何通过

a

ul.ex >

并获得接下来的 6 个ul 通过

b

Here's an HTML snippet:
from : http://www.sphere-light.com/compatibility/maker/id/4/

<h3 class="my-subject">a</h3>

<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>

<h3 class="my-subject">b</h3>

<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>

How can I get just the 6 siblings ul.ex via <h3 class="my-subject">a</h3>

and get the next 6 ul via <h3 class="my-subject">b</h3>?

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

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

发布评论

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

评论(1

您的好友蓝忘机已上羡 2025-01-12 16:57:32

由于

    标签的数量可能会有所不同,因此您可以采取以下几种方法。

使用 XPath:

var h3Xpath = "//h3[@class='my-subject']";
var query = doc.DocumentNode.SelectNodes(h3Xpath);
foreach (var h3 in query)
{
    var value = h3.InnerText;
    var ulXpathFmt = "following-sibling::ul[@class='ex' and " +
        "preceding-sibling::h3[@class='my-subject'][1]='{0}']";
    var uls = h3.SelectNodes(String.Format(ulXpathFmt, value));
    // do something with the uls
}

我通常不会单独使用 XPath 来完成这一切,而是混合使用一些 LINQ。

var h3Xpath = "//h3[@class='my-subject']";
var query = doc.DocumentNode.SelectNodes(h3Xpath);
foreach (var h3 in query)
{
    var ulXpath = "following-sibling::*";
    var uls = h3.SelectNodes(ulXpath)
        .TakeWhile(tag => tag.Name == "ul" &&
                          tag.Attributes["class"].Value == "ex");
    // do something with the uls
}

Since the number of <ul> tags can vary here's a couple of approaches you could take.

Using XPath:

var h3Xpath = "//h3[@class='my-subject']";
var query = doc.DocumentNode.SelectNodes(h3Xpath);
foreach (var h3 in query)
{
    var value = h3.InnerText;
    var ulXpathFmt = "following-sibling::ul[@class='ex' and " +
        "preceding-sibling::h3[@class='my-subject'][1]='{0}']";
    var uls = h3.SelectNodes(String.Format(ulXpathFmt, value));
    // do something with the uls
}

I generally wouldn't do it all using XPath alone but mixed in using some LINQ as well.

var h3Xpath = "//h3[@class='my-subject']";
var query = doc.DocumentNode.SelectNodes(h3Xpath);
foreach (var h3 in query)
{
    var ulXpath = "following-sibling::*";
    var uls = h3.SelectNodes(ulXpath)
        .TakeWhile(tag => tag.Name == "ul" &&
                          tag.Attributes["class"].Value == "ex");
    // do something with the uls
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文