检索PHP中的相对DOM节点 [英] Retrieving relative DOM nodes in PHP

查看:45
本文介绍了检索PHP中的相对DOM节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索文档中下一个元素标签的数据,例如:

I want to retrieve the data of the next element tag in a document, for example:

我想检索< blockquote> ;内容1< / blockquote> 仅适用于每个不同的跨度。

I would like to retrieve <blockquote> Content 1 </blockquote> for every different span only.

<html>
<body>


<span id=12341></span>
<blockquote>Content 1</blockquote>
<blockquote>Content 2</blockquote>

<!-- misc html in between including other spans w/ no relative blockquotes-->

<span id=12342></span>
<blockquote>Content 1</blockquote>

<!-- misc html in between including other spans w/ no relative blockquotes-->

<span id=12343></span>
<blockquote>Content 1</blockquote>
<blockquote>Content 2</blockquote>
<blockquote>Content 3</blockquote>
<blockquote>Content 4</blockquote>

<!-- misc html in between including other spans w/ no relative blockquotes-->    

<span id=12344></span>
<blockquote>Content 1</blockquote>
<blockquote>Content 2</blockquote>
<blockquote>Content 3</blockquote>


</body>
</html>

现在我想知道两件事:

1。)我如何编写一个匹配的表达式,仅输出封闭元素(< span>< / span> )之后紧随其后的块引用?

1.)How can I write an expression that matches and only outputs a blockquote that's followed right after a closed element (<span></span>)?

2。)如果我想要的话,如果我将来有需要在仍适用于

2.)If I wanted, how could I get Content 2, Content 3, etc if I ever have a need to output them in the future while still applying to the rules of the previous question?

推荐答案


现在我想知道两件事:

Now two things I'm wondering:

1。)如何编写匹配的表达式,仅输出封闭的
元素后紧跟的块引用
< ; span>< / span> )?

1.)How can I write an expression that matches and only outputs a blockquote that's followed right after a closed element (<span></span>)?

假定所提供的文本已转换到格式正确的XML文档(您需要将 id 属性的值括在引号中)

Assuming that the provided text is converted to a well-formed XML document (you need to enclose the values of the id attributes in quotes)

使用

/*/*/span/following-sibling::*[1][self::blockquote]

此用英语表示:全选 blockquote 每个元素都是第一个直接的 span 元素是文档顶部元素的孙子

This means in English: Select all blockquote elements each of which is the first, immediate following sibling of a span element that is a grand-child of the top element of the document.


2。)如果我愿意,如果我曾经在
的将来需要输出
,如何获得Content 2,Content 3等。同时仍适用于上一个问题的
规则?

2.)If I wanted, how could I get Content 2, Content 3, etc if I ever have a need to output them in the future while still applying to the rules of the previous question?

span <之后,您可以获得所有连续的 blockquote 元素集/ code>:

You can get all sets of contigious blockquote elements following a span:

 /*/*/span/following-sibling::blockquote
          [preceding-sibling::*[not(self::blockquote)][1][self::span]]

在(N + 1)-st 跨度之后,您可以获得连续的 blockquote 元素集

/*/*/span/following-sibling::blockquote
           [preceding-sibling::*
             [not(self::blockquote)][1]
                [self::span and count(preceding-sibling::span)=$vN]
           ]

其中 $ vN 应为

因此,在第一个之后紧接的 blockquote 元素集 span 选择:

Thus, the set of contigious set of blockquote elements following the first span is selected by:

/*/*/span/following-sibling::blockquote
           [preceding-sibling::*
             [not(self::blockquote)][1]
                [self::span and count(preceding-sibling::span)=0]
           ]

一组连续的 blockquote集在第二个跨度中的元素由选择:

/*/*/span/following-sibling::blockquote
           [preceding-sibling::*
             [not(self::blockquote)][1]
                [self::span and count(preceding-sibling::span)=1]
           ]

。 ...

请参见 XPath Visualizer 由以下表达式选择的节点

See in the XPath Visualizer the nodes selected by the following expression :

/*/*/span/following-sibling::blockquote
           [preceding-sibling::*
             [not(self::blockquote)][1]
                [self::span and count(preceding-sibling::span)=3]
           ]

这篇关于检索PHP中的相对DOM节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆