Using Xpath

Xpath เป็นภาษาที่ใช้ในการเลือก node ที่สนใจจาก XML ซึ่งสะดวกกว่าการใช้การใช้ DOM method ในการเข้าถึง nodeเนื่องจากสามารถเข้าถึง node ที่ต้องการได้ด้วยเพียงคำสั่งเดียว

ตัวอย่าง selector ที่ใช้บ่อยๆได้แก่

selector description
nodename select all child form nodename
/ select leaf node node form root
// select all node node form current node
@ select attributes
[expr] user expression find in specific node

sample xml:

01
<?xml version="1.0" encoding="utf-8"?>
02
<catalog>
03
   <book id="bk101">
04
      <author>Gambardella, Matthew</author>
05
      <title>XML Developer's Guide</title>
06
      <genre>Computer</genre>
07
      <price>44.95</price>
08
      <publish_date>2000-10-01</publish_date>
09
   </book>
10
   <book id="bk102">
11
      <author>Ralls, Kim</author>
12
      <title>Midnight Rain</title>
13
      <genre>Fantasy</genre>
14
      <price>5.95</price>
15
      <publish_date>2000-12-16</publish_date>
16
      <description>A former architect battles corporate zombies, 
17
      an evil sorceress, and her own childhood to become queen 
18
      of the world.</description>
19
   </book>
20
   <book id="bk103">
21
      <author>Corets, Eva</author>
22
      <title>Maeve Ascendant</title>
23
      <genre>Fantasy</genre>
24
      <price>5.95</price>
25
      <publish_date>2000-11-17</publish_date>
26
      <description>After the collapse of a nanotechnology 
27
      society in England, the young survivors lay the 
28
      foundation for a new society.</description>
29
   </book>
30
   <book id="bk104">
31
      <author>Corets, Eva</author>
32
      <title>Oberon's Legacy</title>
33
      <genre>Fantasy</genre>
34
      <price>5.95</price>
35
      <publish_date>2001-03-10</publish_date>
36
   </book>
37
   <book id="bk105">
38
      <author>Corets, Eva</author>
39
      <title>The Sundered Grail</title>
40
      <genre>Fantasy</genre>
41
      <price>5.95</price>
42
      <publish_date>2001-09-10</publish_date>
43
   </book>
44
   <book id="bk106">
45
      <author>Randall, Cynthia</author>
46
      <title>Lover Birds</title>
47
      <genre>Romance</genre>
48
      <price>4.95</price>
49
      <publish_date>2000-09-02</publish_date>
50
      <description>When Carla meets Paul at an ornithology 
51
      conference, tempers fly as feathers get ruffled.</description>
52
   </book>
53
   <book id="bk107">
54
      <author>Thurman, Paula</author>
55
      <title>Splish Splash</title>
56
      <genre>Romance</genre>
57
      <price>4.95</price>
58
      <publish_date>2000-11-02</publish_date>
59
   </book>
60
   <book id="bk108">
61
      <author>Knorr, Stefan</author>
62
      <title>Creepy Crawlies</title>
63
      <genre>Horror</genre>
64
      <price>4.95</price>
65
      <publish_date>2000-12-06</publish_date>
66
   </book>
67
   <book id="bk109">
68
      <author>Kress, Peter</author>
69
      <title>Paradox Lost</title>
70
      <genre>Science Fiction</genre>
71
      <price>6.95</price>
72
      <publish_date>2000-11-02</publish_date>
73
   </book>
74
   <book id="bk110">
75
      <author>O'Brien, Tim</author>
76
      <title>Microsoft .NET: The Programming Bible</title>
77
      <genre>Computer</genre>
78
      <price>36.95</price>
79
      <publish_date>2000-12-09</publish_date>
80
   </book>
81
   <book id="bk111">
82
      <author>O'Brien, Tim</author>
83
      <title>MSXML3: A Comprehensive Guide</title>
84
      <genre>Computer</genre>
85
      <price>36.95</price>
86
      <publish_date>2000-12-01</publish_date>
87
   </book>
88
   <book id="bk112">
89
      <author>Galos, Mike</author>
90
      <title>Visual Studio 7: A Comprehensive Guide</title>
91
      <genre>Computer</genre>
92
      <price>49.95</price>
93
      <publish_date>2001-04-16</publish_date>
94
   </book>
95
</catalog>

sample to use Xpath by php:

01
<?php
02
function getSelectXpathValue(&$xpath,$expression){
03
	$obj = $xpath->xpath_eval($expression);
04
	$value = array();
05
	if(@count($obj->nodeset)){ 
06
		foreach($obj->nodeset as $node){
07
			$value[] = $node->get_content();
08
		}
09
	}
10
	return $value;
11
}
12
 
13
$xml = domxml_open_file('books.xml');
14
$xpath = &$xml->xpath_new_context();
15
 
16
$value = getSelectXpathValue($xpath, "/catalog/book[@id='bk102']");
17
print_r($value);
18
/* output is:
19
Array
20
(
21
    [0] => 
22
      Ralls, Kim
23
      Midnight Rain
24
      Fantasy
25
      5.95
26
      2000-12-16
27
      A former architect battles corporate zombies, 
28
      an evil sorceress, and her own childhood to become queen 
29
      of the world.
30
   
31
)
32
*/
33
$value = getSelectXpathValue($xpath, "/catalog/book[genre='Fantasy'][description]/title");
34
print_r($value);
35
/* output is:
36
Array
37
(
38
    [0] => Midnight Rain
39
    [1] => Maeve Ascendant
40
)
41
*/
42
$value = getSelectXpathValue($xpath, "//title");
43
print_r($value);
44
/* output is:
45
Array
46
(
47
    [0] => XML Developer's Guide
48
    [1] => Midnight Rain
49
    [2] => Maeve Ascendant
50
    [3] => Oberon's Legacy
51
    [4] => The Sundered Grail
52
    [5] => Lover Birds
53
    [6] => Splish Splash
54
    [7] => Creepy Crawlies
55
    [8] => Paradox Lost
56
    [9] => Microsoft .NET: The Programming Bible
57
    [10] => MSXML3: A Comprehensive Guide
58
    [11] => Visual Studio 7: A Comprehensive Guide
59
)
60
*/
61
?>

Leave a Reply