English 中文(简体)
php xpath table parsing question
原标题:php xpath table parsing question
  • 时间:2011-01-05 18:42:25
  •  标签:
  • php
  • xpath

我在一张桌子上开了几张桌子,我使用的是:php×path。

I m using a series of xpaths because I m breaking up the code into conceptual units across several methods calls, and this structure has been working perfectly in other scenarios without nested tables.

该法典:

// create a host DOM document
$dom = new DOMDocument();

// load the html string into the dom
$dom->loadHTML($html_string);

// make an xpath object out of the dom
$xpath = new DOMXpath($dom);

// run query to extract the rows from the master table
$context_nodes = $xpath->query( //table[@id="id1"]/tr[position()>1] );

// parse data from the individual tables nested in each master table row
foreach($context_nodes as $context_node){
    $interesting_nodes[] = $xpath->query( table[2]/tr[td[2]] , $context_node);
}

由此产生的利息——计票阵列中有空的DOMNodeLists。

斜体是DOMNodeList,含有有效数据。 每一美元目录的html内容就这样:

<td>
    <table></table>
    <table>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
        </tr>
    </table>
</td>

I tried the following simplified $intesting_nodes query to match any table:

$intesting_nodes[] = $xpath->query( table , $context_node);

但是,这仍然产生同样的空洞的DOMNodeLists。

www.un.org/Depts/DGACM/index_spanish.htm 现在有趣的部分是。

当我尝试一笔利息——不问:

$interesting_nodes[] = $xpath->query( *[2]/*[*[2]] , $context_node);

Then everything works perfectly; but if I replace any "*" with the corresponding "table", "tr", or "td" tags, then the query breaks once again.

是否有任何其他人在这种行为方面经历过?

我非常希望能够利用更确切的问询,并且希望能够像现在这样保持问答,而不是把它变成绝对的。

最佳回答

我表示:

如果主桌子没有座标,则主子的Xpath执行并不了解表内节点(即:tr、 t)是什么。

My outer td tags were causing unexpected results from the xpath query.

3. 修改案文:

$context_nodes = $xpath->query( //table[@id="id1"]/tr[position()>1]/td );

而且,我们是好的。

问题回答

我认为,也许你们需要在随后的询问中使用一条相对的道路(由......编辑),见





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签