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 queen18 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 nanotechnology27 society in England, the young survivors lay the28 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 ornithology51 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 <?php02 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 Array20 (21 [0] =>22 Ralls, Kim23 Midnight Rain24 Fantasy25 5.9526 2000-12-1627 A former architect battles corporate zombies,28 an evil sorceress, and her own childhood to become queen29 of the world.30 31 )32 */33 $value = getSelectXpathValue($xpath, "/catalog/book[genre='Fantasy'][description]/title");34 print_r($value);35 /* output is:36 Array37 (38 [0] => Midnight Rain39 [1] => Maeve Ascendant40 )41 */42 $value = getSelectXpathValue($xpath, "//title");43 print_r($value);44 /* output is:45 Array46 (47 [0] => XML Developer's Guide48 [1] => Midnight Rain49 [2] => Maeve Ascendant50 [3] => Oberon's Legacy51 [4] => The Sundered Grail52 [5] => Lover Birds53 [6] => Splish Splash54 [7] => Creepy Crawlies55 [8] => Paradox Lost56 [9] => Microsoft .NET: The Programming Bible57 [10] => MSXML3: A Comprehensive Guide58 [11] => Visual Studio 7: A Comprehensive Guide59 )60 */61 ?>