1 <?php
2 $doc = new DOMDocument('1.0', 'utf-8');
3 $doc->load('./info.xml');
4 $xpath = new DOMXPath($doc);
5 $name = $_GET['name'];
6 $password = $_GET['password'];
7 $query = "/addressBook/address[name/text()='$name' and password/text()='$password']/card";
8 echo $query."<br>";
9 $arts = $xpath->query($query);
10 foreach ($arts as $art)
11 {
12 echo $art->nodeValue." ";
13 }
14 ?>
其中,articles.xml的内容如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <articles>
3 <article id="1">
4 <tags>
5 <tag>php</tag>
6 <tag>xpath</tag>
7 </tags>
8 <title>PHP XPath Example</title>
9 </article>
10 <article id="2">
11 <tags>
12 <tag>dom</tag>
13 <tag>dodocument</tag>
14 </tags>
15 <title>DomDocument Tutorial</title>
16 </article>
17 </articles>