-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathXPath_Example.php
More file actions
59 lines (54 loc) · 1.45 KB
/
XPath_Example.php
File metadata and controls
59 lines (54 loc) · 1.45 KB
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
59
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>XPath Example using PHP and SimpleXMLElement</title>
</head>
<body>
<?php
/*
* Sample Book Store XML manipulation using XPath
*/
$SAMPLE_RESPONSE= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book>
<title lang="en">Bitterness (An African Novel from Zambia)</title>
<author>Malama Katulwende </author>
<year>2005</year>
<price>14.66</price>
</book>
<book>
<title lang="en">Glimmers of Hope : A Memoir of Zambia</title>
<author>Mark Burke</author>
<year>2010</year>
<price>15.98</price>
</book>
<book>
<title lang="en">Salaula: The World of Secondhand Clothing and Zambia</title>
<author>Karen Tranberg Hansen</author>
<year>2000</year>
<price>30.60</price>
</book>
</bookstore>
XML;
$response_xml = new SimpleXMLElement(strstr($SAMPLE_RESPONSE, '<'));
echo '<hr>';
foreach ($response_xml->xpath('//bookstore/book') as $book) {
foreach ($book->children() as $book_details) {
if (!empty($book_details)) {
echo $book_details->getName() . ": " . strval($book_details) . "<br>";
}
}
echo '<hr>';
}
?>
</body>
</html>