E4X按文本内容过滤?

发布于 2024-07-23 06:04:01 字数 478 浏览 6 评论 0原文

我正在尝试使用 E4X 在 JavaScript 中过滤一些 XML,并且有一些特定的需求。 鉴于以下情况:

    var xml = <body>
      <div>
        <p>This is some text that I have.</p>
      </div>
    </div>;

我想在文档中搜索以“这是一些文本”开头的段落。

目前,我可以通过以下内容来获取该段落:

xml..div(p.text().toString().indexOf("This is some text") === 0)

但是,“真正的”XML 要复杂得多(想想:常规网页)。 不保证有一个 div 直接作为问题中的段落的父级。 同一父元素中相关段落之前/之后可能还有其他段落。

有任何想法吗?

I'm trying to filter some XML in JavaScript using E4X and have some specific needs. Given the following:

    var xml = <body>
      <div>
        <p>This is some text that I have.</p>
      </div>
    </div>;

I want to search the document for paragraphs starting with "This is some text".

Currently I can the following to get at the paragraph:

xml..div(p.text().toString().indexOf("This is some text") === 0)

However, the "real" XML is much more complex (think: a regular web page). There is no guarantee that there will be a div directly parenting the paragraph(s) in questions. There may be other paragraphs before/after the paragraph(s) in question within the same parent element.

Any ideas?

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

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

发布评论

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

评论(1

淤浪 2024-07-30 06:04:01

使用双点运算符搜索所有

任何嵌套级别的节点,然后过滤掉节点以仅包含以所需字符串开头的节点。 你已经很接近了,你只需要去掉

即可。 因为,正如您所说,

; 可能不存在:
xml..p.( text().toString().indexOf("This is some text") == 0 );

Use the double dot operator to search for all <p> nodes of any nesting leveling and then filter out the nodes to only include ones that start with your desired string. You were close, you just need to get rid of the <div> since, as you've stated, the <div> might not be present.:

xml..p.( text().toString().indexOf("This is some text") == 0 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文