Board logo

标题: 使用 PHP 解析 RDDL 文档 更新 [打印本页]

作者: look_w    时间: 2018-7-11 08:48     标题: 使用 PHP 解析 RDDL 文档 更新

使用 PHP 访问 RDDL 信息得到包含 RDDL 资源定义的 XHTML 文档之后,使用 XML_RDDL 访问其中的各类信息很简单。 示范了使用 PHP 从 XHTML 文档中检索全部 RDDL 资源的过程:
清单 2. 使用 PHP 解析 RDDL 数据
1
2
3
4
5
6
7
8
9
10
11
12
<?php
// include class file
include 'XML/RDDL.php';

// create RDDL parser
// parse RDDL file
$rddl = new XML_RDDL();
$rddl->parseRDDL('example.html');

// print array of resources
print_r($rddl->getAllResources());
?>




使用 PHP XML_RDDL 包读取  所示的 XHTML 文档并检索所有的资源。首先读入 XML_RDDL 类文件,初始化 XML_RDDL 类的一个实例。然后用该类的 parseRDDL() 方法解析源文件(可以是本地文件或者远程 URL)。文档解析之后,getAllResources() 方法用关联数组返回文档中所有的 <resource> 元素列表。
显示了  输出结果的片断:
清单 3. 清单 2 的输出结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Array
(
[0] => Array
(
[lang] => en
[type] => simple
[href] => http://app.example.domain/example.xsd
[role] => http://www.w3.org/2000/10/XMLSchema
[title] => Example XML Schema
[arcrole] => http://www.rddl.org/purposes#schema-validation
)

[1] => Array
(
[lang] => en
[type] => simple
[id] => dtd
[href] => http://app.example.domain/example.dtd
[role] => http://www.isi.edu/in-notes/iana/assignments/media-types/
application/xml-dtd
[title] => Example DTD
[arcrole] => http://www.rddl.org/purposes#validation
)
...
)




利用 PHP foreach() 循环很容易改变数组的格式,以用 HTML 显示出来。 示范了这个过程,结果如  所示。
清单 4. 将 RDDL 数据格式化为表格
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<style type="text/css">
table {
width:100%;
border-collapse:collapse;
}
td {
border: solid 1px black;
padding: 5px;
}
</style>
</head>
<body>
<h2>Resources</h2>
<?php
// include class file
include 'XML/RDDL.php';

// create RDDL parser
// parse RDDL file
$rddl = new XML_RDDL();
$rddl->parseRDDL('example.html');

// get all resources as array
// format as table
$resources = $rddl->getAllResources();
if (is_array($resources) && count($resources) > 0) {
?>
<table>
<tr>
<td>Resource</td>
<td>Description</td>
<td>Purpose</td>
<td>Role</td>
</tr>
<?php
foreach ($resources as $r) {
$purpose = explode('#', $r['arcrole']);
?>
<tr>
<td><a href="<?php echo $r['href']; ?>"><?php echo $r['href']; ?></a></td>
<td><?php echo $r['title']; ?></td>
<td><?php echo $purpose[1]; ?></td>
<td><a href="<?php echo $r['role']; ?>"><?php echo $r['role']; ?></a></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
</body>
</html>




图 1. 用 RDDL 数据创建的 Web 页面按照性质或用途挑选资源上面所用的 getAllResources() 方法返回源文件中所有的资源。但通常要求更复杂一点:比如返回用于 validation 的所有资源或者具有特定性质的资源。XML_RDDL 包也为这些需要提供了相应的方法。 显示了其中的一部分:
清单 5. 检索资源子集
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rddl="http://www.rddl.org/"
xml:lang="en">
<head>
<title>An Example RDDL Document</title>
</head>
<body>
<pre>
<?php
// include class file
include 'XML/RDDL.php';

// create RDDL parser
// parse RDDL file
$rddl = new XML_RDDL();
$rddl->parseRDDL('example.html');

// get resources by nature
// get all DTDs
echo "Resources by nature:\n";
foreach ($rddl->getResourcesByNature('http://www.isi.edu/in-notes/iana/assignments/
media-types/application/xml-dtd') as $r) {
echo $r['href'] . " \n";
}
echo "\n";

// get resources by purpose
// get all software packages
echo "Resources by purpose:\n";
foreach ($rddl->getResourcesByPurpose('http://www.rddl.org/
purposes#software-package') as $r) {
echo $r['href'] . " \n";
}
echo "\n";

// get a specific resource using its id
$dtd = $rddl->getResourceById('dtd');
if (is_array($dtd)) {
echo "Resource with id 'dtd':\n";
echo $dtd['href'];
}
?>
</pre>
</body>
</html>




示范了三个重要的方法:getResourcesByNature() 根据某种特性 URI 返回全部具有该特性的资源;getResourcesByPurpose() 返回适合特定用途的所有资源;getResourceById() 根据 ID 返回和该标识符匹配的资源。这些方法适合检索符合特定条件的资源。
显示了  的输出结果:
图 2. 清单 5 返回的资源子集上述例子表明,XML_RDDL 包提供一个非常有用的 PHP 工具,可以快速访问 XHTML+RDDL 文档中关于资源的特定信息片断。下一次遇到这样的文档时不妨试一试,看看效果如何!




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0