QueryPath PHP 中的jQuery


Posted in PHP onApril 11, 2010

官方主页  http://querypath.org/

QueryPath(QP)库 在 PHP 中实现了类似于 jQuery 的效果,用它还可以方便地处理 XML HTML...功能太强大了!!!

A QueryPath Tutorial(一个简易说明)
QueryPath makes use of method chaining to provide a concise suite of tools for manipulating a DOM.
The basic principle of method chaining is that each method returns an object upon which additional methods can be called. In our case, the QueryPath object usually returns itself.
Let's take a look at an example to illustrate:
$qp = qp(QueryPath::HTML_STUB); // Generate a new QueryPath object.(创建一个 QP 对象)
$qp2 = $qp->find('body'); // Find the body tag.(找到 "body" 标签)
// Now the surprising part:(请看下面让你惊奇的地方)
if ($qp === $qp2) {
// This will always get printed.(它总是会这样输出)
print "MATCH";
}
Why does $qp always equal $qp2? Because the find() function does all of its data gathering and then returns the QueryPath object.
This might seem esoteric, but it all has a very practical rationale. With this sort of interface, we can chain lots of methods together:
(你可以向使用 jQuery 一样来连缀方法)
qp(QueryPath::HTML_STUB)->find('body')->text('Hello World')->writeHTML();
In this example, we have four method calls:
qp(QueryPath::HTML_STUB): Create a new QueryPath object and provide it with a stub of an HTML document. This returns the QueryPath object.
find('body'): This searches the QueryPath document looking for an element named 'body'. That element is, of course, the <body></body> portion of the HTML document. When it finds the body element, it keeps an internal pointer to that element, and it returns the QueryPath object (which is now wrapping the body element).
text('Hello World'): This function takes the current element(s) wrapped by QueryPath and adds the text Hello World. As you have probably guessed, it, too, returns a QueryPath object. The object will still be pointing to the body element.
writeHTML(): The writeHTML() function prints out the entire document. This is used to send the HTML back to the client. You'll never guess what this function returns. Okay, you guessed it. QueryPath.
So at the end of the chain above, we would have created a document that looks something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> 
<title>Untitled</title> 
</head> 
<body>Hello World</body> 
</html>

Most of that HTML comes from the QueryPath::HTML_STUB. All we did was add the Hello World text inside of the <body></body> tags.
Not all QueryPath functions return QueryPath objects. Some tools need to return other data. But those functions are well-documented in the included documentation.
These are the basic principles behind QueryPath. Now let's take a look at a larger example that exercises more of the QueryPath API.
A Longer Example
This example illustrates various core features of QueryPath.
In this example, we use some of the standard QueryPath functions (most of them implementing the jQuery interface) to build a new web page from scratch.
Each line of the code has been commented individually. The output from this is shown in a separate block beneath.
<?php 
/** 
* Using QueryPath. 
* 
* This file contains an example of how QueryPath can be used 
* to generate web pages. 
* @package QueryPath 
* @subpackage Examples 
* @author M Butcher <matt@aleph-null.tv> 
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license. 
*/ 
// Require the QueryPath core. 
require_once 'QueryPath/QueryPath.php'; 
// Begin with an HTML stub document (XHTML, actually), and navigate to the title. 
qp(QueryPath::HTML_STUB, 'title') 
// Add some text to the title 
->text('Example of QueryPath.') 
// Now look for the <body> element 
->find(':root body') 
// Inside the body, add a title and paragraph. 
->append('<h1>This is a test page</h1><p>Test text</p>') 
// Now we select the paragraph we just created inside the body 
->children('p') 
// Add a 'class="some-class"' attribute to the paragraph 
->attr('class', 'some-class') 
// And add a style attribute, too, setting the background color. 
->css('background-color', '#eee') 
// Now go back to the paragraph again 
->parent() 
// Before the paragraph and the title, add an empty table. 
->prepend('<table id="my-table"></table>') 
// Now let's go to the table... 
->find('#my-table') 
// Add a couple of empty rows 
->append('<tr></tr><tr></tr>') 
// select the rows (both at once) 
->children() 
// Add a CSS class to both rows 
->addClass('table-row') 
// Now just get the first row (at position 0) 
->eq(0) 
// Add a table header in the first row 
->append('<th>This is the header</th>') 
// Now go to the next row 
->next() 
// Add some data to this row 
->append('<td>This is the data</td>') 
// Write it all out as HTML 
->writeHTML(); 
?>

The code above produces the following HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> 
<title>Example of QueryPath.</title> 
</head> 
<body> 
<table id="my-table"> 
<tr class="table-row"><th>This is the header</th></tr> 
<tr class="table-row"><td>This is the data</td></tr> 
</table> 
<h1>This is a test page</h1> 
<p class="some-class" style="background-color: #eee">Test text</p></body> 
</html>

Now you should have an idea of how QueryPath works. Grab a copy of the library and try it out! Along with the source code, you will get a nice bundle of HTML files that cover every single public function in the QueryPath library (no kidding). There are more examples there, too.
不错的东东!赶紧 Grab 它吧~~!
PHP 相关文章推荐
JAVA/JSP学习系列之七
Oct 09 PHP
php将数据库导出成excel的方法
May 07 PHP
php中邮箱地址正则表达式实现与详解
Apr 24 PHP
php中禁止单个IP与ip段访问的代码小结
Jul 04 PHP
Win2003+apache+PHP+SqlServer2008 配置生产环境
Jul 29 PHP
ThinkPHP添加更新标签的方法
Dec 05 PHP
php中array_multisort对多维数组排序的方法
Jun 21 PHP
适合PHP初学者阅读的4本经典书籍
Sep 23 PHP
php封装的数据库函数与用法示例【参考thinkPHP】
Nov 08 PHP
php自定义函数br2nl实现将html中br换行符转换为文本输入中换行符的方法【与函数nl2br功能相反】
Feb 17 PHP
Yii2表单事件之Ajax提交实现方法
May 04 PHP
php使用ftp实现文件上传与下载功能
Jul 21 PHP
10个可以简化php开发过程的MySQL工具
Apr 11 #PHP
Fatal error: Call to undefined function curl_init()解决方法
Apr 09 #PHP
PHP Socket 编程
Apr 09 #PHP
有关JSON以及JSON在PHP中的应用
Apr 09 #PHP
dedecms系统的广告设置代码 基础版本
Apr 09 #PHP
PHP 动态随机生成验证码类代码
Apr 09 #PHP
DedeCMS 核心类TypeLink.class.php摘要笔记
Apr 07 #PHP
You might like
php上传文件中文文件名乱码的解决方法
2013/11/01 PHP
PHP如何通过传引用的思想实现无限分类(代码简单)
2015/10/13 PHP
XHProf报告字段含义的解析
2016/05/17 PHP
如何在centos8自定义目录安装php7.3
2019/11/28 PHP
JavaScript获取当前日期是星期几的方法
2015/04/06 Javascript
浅谈JavaScript中null和undefined
2015/07/09 Javascript
jquery实现表格隔行换色效果
2015/11/19 Javascript
简述Matlab中size()函数的用法
2016/03/20 Javascript
js和jq使用submit方法无法提交表单的快速解决方法
2016/05/17 Javascript
JavaScript 数组- Array的方法总结(推荐)
2016/07/21 Javascript
jQuery包裹节点用法完整示例
2016/09/13 Javascript
使用JS批量选中功能实现更改数据库中的status状态值(批量展示)
2016/11/22 Javascript
jquery uploadify隐藏上传进度的实现方法
2017/02/06 Javascript
Webpack中css-loader和less-loader的使用教程
2017/04/27 Javascript
vue事件修饰符和按键修饰符用法总结
2017/07/25 Javascript
Vue下的国际化处理方法
2017/12/18 Javascript
Vue组件之自定义事件的功能图解
2018/02/01 Javascript
详解开发react应用最好用的脚手架 create-react-app
2018/04/24 Javascript
详解vue的双向绑定原理及实现
2019/05/05 Javascript
BootstrapValidator验证用户名已存在(ajax)
2019/11/08 Javascript
[02:43]DOTA2亚洲邀请赛场馆攻略——带你走进东方体育中心
2018/03/19 DOTA
python 2.6.6升级到python 2.7.x版本的方法
2016/10/09 Python
浅谈Python中带_的变量或函数命名
2017/12/04 Python
Python框架Flask的基本数据库操作方法分析
2018/07/13 Python
基于python代码实现简易滤除数字的方法
2018/07/17 Python
django框架防止XSS注入的方法分析
2019/06/21 Python
浅谈pycharm使用及设置方法
2019/09/09 Python
2014新课程改革心得体会
2014/03/10 职场文书
创文明城市标语
2014/06/16 职场文书
离职证明范本(5篇)
2014/09/19 职场文书
优秀学生干部事迹材料
2014/12/24 职场文书
岳庙导游词
2015/02/04 职场文书
2015社区六五普法工作总结
2015/04/21 职场文书
婚庆开业庆典主持词
2015/06/30 职场文书
班委竞选稿范文
2015/11/21 职场文书
使用Ajax实现进度条的绘制
2022/04/07 Javascript