【发布时间】:2016-10-04 23:06:49
【问题描述】:
这是我的 xml 文件和我的 php 代码。我想通过我的 php 表单中的文本框按名字搜索,我想显示有关特定学生的所有信息,但问题是我的 xpath 不起作用。任何有关 xpath 的帮助。
<students>
<student>
<firstname>John</firstname>
<lasttname>Snow</lasttname>
<student_id>160600</student_id>
<gender>male</gender>
<dob>23-06-95</dob>
<age>21</age>
<email>JohnSnow@gmail.com</email>
<mobilenumber>57675060</mobilenumber>
<address>Winter Fel</address>
<cohort>BSE15PT</cohort>
<programme>Software Engineering</programme>
<mode>PT</mode>
</student>
<student>
<firstname>meryl</firstname>
<lastname>Burton</lastname>
<student_id>150500</student_id>
<gender>female</gender>
<dob>26-07-95</dob>
<email>mirena@gmail.com</email>
<mobilenumber>57800603</mobilenumber>
<address>rose hill</address>
<cohort>BSE15AFT</cohort>
<programme>software engineering</programme>
<mode>ft</mode>
</student>
</students>
<?php
if(isset($_POST['search']))
{
$xml=simplexml_load_file("studentInstance.xml") or die("Error: Cannot Create Object");
$xpath = $xml;
//query the document
$name = $_POST['studentname'];
$query = $xpath->query("/students/student/[firstname = '$name']");
echo $query;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Searching</title>
</head>
<body>
<form method="POST" action="searchRecord.php">
<label>Enter Student Name</label>
<input type="text" name="studentname"><br>
<input type="submit" name="search" value="search">
</form>
</body>
</html>
【问题讨论】:
-
我不确定,我在手机里,但学生是一个列表,我认为您可能需要特定索引 /students/student[1]/firstname
标签: php xml forms xpath xml-parsing