Intereting Posts

изменить файл xml через php-форму

Это мой xml-файл и мой php-код ниже. Я поместил тип ввода, который будет искать ученика по первому имени. Затем появится информация о конкретном ученике и появится другое обновление кнопки.

Проблема в том, что я хочу изменить информацию после этого. Как я могу получить элемент по имени тега, чтобы изменить информацию о конкретном ученике?

<students> <student> <firstname>John</firstname> <lasttname>Snow</lasttname> <student_id>160600</student_id> <gender>male</gender> <dob>23-06-95</dob> <email>JohnSnow@gmail.com</email> <mobilenumber>57675060</mobilenumber> <address>albatros, portlouis</address> <cohort>BSE15PT</cohort> <programme>Software Engineering</programme> <mode>PT</mode> </student> <student> <firstname>Jey</firstname> <lastname>Lacroix</lastname> <student_id>150501</student_id> <gender>M</gender> <dob>1990-02-22</dob> <email>Jey@hotmail.com</email> <mobilenumber>57553536</mobilenumber> <address>Curepipe</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"); //query the document $name = $_POST['studentname']; //$xml = simplexml_load_string($xml); $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; //echo "<pre>"; //rint_r($array); //echo "</pre>"; $count=0; $size=count($array); //echo $count; echo "<center>"; while($count!=count($array)){ foreach ($array[$count]->children() as $child) {//stores values in child $getElementTag=$child->getName();//get tag so nom echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" value= " '.$child.' " size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "***************************"; echo "</center>"; } ?> <!DOCTYPE html> <html> <head> <title>Searching</title> </head> <body> <center> <form method="POST" action="searchtest.php"> <label>Enter Student Name</label> <input type="text" name="studentname" pattern="[AZ][az]+" title="Must start with capital letters!" required><br> <br> <input type="submit" name="search" value="search"> </form> </center> </body> </html> -  <?php if(isset($_POST['search'])) { $xml=simplexml_load_file("studentInstance.xml") or die("Error: Cannot Create Object"); //query the document $name = $_POST['studentname']; //$xml = simplexml_load_string($xml); $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; //echo "<pre>"; //rint_r($array); //echo "</pre>"; $count=0; $size=count($array); //echo $count; echo "<center>"; while($count!=count($array)){ foreach ($array[$count]->children() as $child) {//stores values in child $getElementTag=$child->getName();//get tag so nom echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" value= " '.$child.' " size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "***************************"; echo "</center>"; } ?> <!DOCTYPE html> <html> <head> <title>Searching</title> </head> <body> <center> <form method="POST" action="searchtest.php"> <label>Enter Student Name</label> <input type="text" name="studentname" pattern="[AZ][az]+" title="Must start with capital letters!" required><br> <br> <input type="submit" name="search" value="search"> </form> </center> </body> </html> в  <?php if(isset($_POST['search'])) { $xml=simplexml_load_file("studentInstance.xml") or die("Error: Cannot Create Object"); //query the document $name = $_POST['studentname']; //$xml = simplexml_load_string($xml); $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; //echo "<pre>"; //rint_r($array); //echo "</pre>"; $count=0; $size=count($array); //echo $count; echo "<center>"; while($count!=count($array)){ foreach ($array[$count]->children() as $child) {//stores values in child $getElementTag=$child->getName();//get tag so nom echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" value= " '.$child.' " size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "***************************"; echo "</center>"; } ?> <!DOCTYPE html> <html> <head> <title>Searching</title> </head> <body> <center> <form method="POST" action="searchtest.php"> <label>Enter Student Name</label> <input type="text" name="studentname" pattern="[AZ][az]+" title="Must start with capital letters!" required><br> <br> <input type="submit" name="search" value="search"> </form> </center> </body> </html> 

Рассмотрим динамический XSLT (язык преобразования, используемый для изменения XML-документов), где значения формы передаются скрипту XSLT. Однако в текущем скрипте необходимо изменить несколько элементов:

  1. Тебе нужны теги <form> чтобы инициировать массив $_POST и отправлять значения на сервер. Добавьте необходимые действия ниже.
  2. Вам нужно предоставить каждому <input> отдельное имя, которое у вас уже есть с $ getElementTag . Рассмотрите возможность $_POST значений $_POST на стороне сервера.
  3. Вам нужно скрытое поле ввода, чтобы сохранить старое имя, если пользователь изменит это значение. Это поле важно, поскольку оно используется в XSLT для выбора соответствующего узла <student> для обновления.

PHP Script

Ниже скрипт содержит только два условия if (isset(...) . Интегрируйте в свой полный скрипт и убедитесь, что эхо не отображается над <html> и <head> . Кроме того, включена встроенная строка XSLT. расширение XSLTProcessor (php_xsl.dll или php_xsl.so) включено в .ini-файле.

 <?php $xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object"); if (isset($_POST['modify'])) { $oldfirstname=($_POST['oldfirstname']); $firstname=($_POST['firstname']); $lastname=($_POST['lastname']); $student_id=($_POST['student_id']); $gender=($_POST['gender']); $dob=($_POST['dob']); $email=($_POST['email']); $mobilenumber=($_POST['mobilenumber']); $address=($_POST['address']); $cohort=($_POST['cohort']); $programme=($_POST['programme']); $mode=($_POST['mode']); $xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="student[firstname=\''.$oldfirstname.'\']"> <xsl:copy> <firstname>'.$firstname.'</firstname> <lasttname>'.$lastname.'</lasttname> <student_id>'.$student_id.'</student_id> <gender>'.$gender.'</gender> <dob>'.$dob.'</dob> <email>'.$email.'</email> <mobilenumber>'.$mobilenumber.'</mobilenumber> <address>'.$address.'</address> <cohort>'.$cohort.'</cohort> <programme>'.$programme.'</programme> <mode>'.$mode.'</mode> </xsl:copy> </xsl:template> </xsl:transform>'; $xsl = new DOMDocument; $xsl->loadXML($xslstr); // Configure the transformer $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); // Transform XML source $newXml = $proc->transformToXML($xml); // Save into new file file_put_contents($cd."/FormInput_php.xml", $newXml); } if(isset($_POST['search'])) { //query the document $name = $_POST['studentname']; $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; $count=0; $size=count($array); echo "<center>"; echo '<form id="contactform" name="contactform" method="post">'; while($count!=count($array)){ foreach ($array[$count]->children() as $child) { $getElementTag=$child->getName(); echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>'; echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "</form>"; echo "***************************"; echo "</center>"; } ?> номер <?php $xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object"); if (isset($_POST['modify'])) { $oldfirstname=($_POST['oldfirstname']); $firstname=($_POST['firstname']); $lastname=($_POST['lastname']); $student_id=($_POST['student_id']); $gender=($_POST['gender']); $dob=($_POST['dob']); $email=($_POST['email']); $mobilenumber=($_POST['mobilenumber']); $address=($_POST['address']); $cohort=($_POST['cohort']); $programme=($_POST['programme']); $mode=($_POST['mode']); $xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="student[firstname=\''.$oldfirstname.'\']"> <xsl:copy> <firstname>'.$firstname.'</firstname> <lasttname>'.$lastname.'</lasttname> <student_id>'.$student_id.'</student_id> <gender>'.$gender.'</gender> <dob>'.$dob.'</dob> <email>'.$email.'</email> <mobilenumber>'.$mobilenumber.'</mobilenumber> <address>'.$address.'</address> <cohort>'.$cohort.'</cohort> <programme>'.$programme.'</programme> <mode>'.$mode.'</mode> </xsl:copy> </xsl:template> </xsl:transform>'; $xsl = new DOMDocument; $xsl->loadXML($xslstr); // Configure the transformer $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); // Transform XML source $newXml = $proc->transformToXML($xml); // Save into new file file_put_contents($cd."/FormInput_php.xml", $newXml); } if(isset($_POST['search'])) { //query the document $name = $_POST['studentname']; $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; $count=0; $size=count($array); echo "<center>"; echo '<form id="contactform" name="contactform" method="post">'; while($count!=count($array)){ foreach ($array[$count]->children() as $child) { $getElementTag=$child->getName(); echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>'; echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "</form>"; echo "***************************"; echo "</center>"; } ?> номер <?php $xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object"); if (isset($_POST['modify'])) { $oldfirstname=($_POST['oldfirstname']); $firstname=($_POST['firstname']); $lastname=($_POST['lastname']); $student_id=($_POST['student_id']); $gender=($_POST['gender']); $dob=($_POST['dob']); $email=($_POST['email']); $mobilenumber=($_POST['mobilenumber']); $address=($_POST['address']); $cohort=($_POST['cohort']); $programme=($_POST['programme']); $mode=($_POST['mode']); $xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="student[firstname=\''.$oldfirstname.'\']"> <xsl:copy> <firstname>'.$firstname.'</firstname> <lasttname>'.$lastname.'</lasttname> <student_id>'.$student_id.'</student_id> <gender>'.$gender.'</gender> <dob>'.$dob.'</dob> <email>'.$email.'</email> <mobilenumber>'.$mobilenumber.'</mobilenumber> <address>'.$address.'</address> <cohort>'.$cohort.'</cohort> <programme>'.$programme.'</programme> <mode>'.$mode.'</mode> </xsl:copy> </xsl:template> </xsl:transform>'; $xsl = new DOMDocument; $xsl->loadXML($xslstr); // Configure the transformer $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); // Transform XML source $newXml = $proc->transformToXML($xml); // Save into new file file_put_contents($cd."/FormInput_php.xml", $newXml); } if(isset($_POST['search'])) { //query the document $name = $_POST['studentname']; $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; $count=0; $size=count($array); echo "<center>"; echo '<form id="contactform" name="contactform" method="post">'; while($count!=count($array)){ foreach ($array[$count]->children() as $child) { $getElementTag=$child->getName(); echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>'; echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "</form>"; echo "***************************"; echo "</center>"; } ?> по <?php $xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object"); if (isset($_POST['modify'])) { $oldfirstname=($_POST['oldfirstname']); $firstname=($_POST['firstname']); $lastname=($_POST['lastname']); $student_id=($_POST['student_id']); $gender=($_POST['gender']); $dob=($_POST['dob']); $email=($_POST['email']); $mobilenumber=($_POST['mobilenumber']); $address=($_POST['address']); $cohort=($_POST['cohort']); $programme=($_POST['programme']); $mode=($_POST['mode']); $xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="student[firstname=\''.$oldfirstname.'\']"> <xsl:copy> <firstname>'.$firstname.'</firstname> <lasttname>'.$lastname.'</lasttname> <student_id>'.$student_id.'</student_id> <gender>'.$gender.'</gender> <dob>'.$dob.'</dob> <email>'.$email.'</email> <mobilenumber>'.$mobilenumber.'</mobilenumber> <address>'.$address.'</address> <cohort>'.$cohort.'</cohort> <programme>'.$programme.'</programme> <mode>'.$mode.'</mode> </xsl:copy> </xsl:template> </xsl:transform>'; $xsl = new DOMDocument; $xsl->loadXML($xslstr); // Configure the transformer $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); // Transform XML source $newXml = $proc->transformToXML($xml); // Save into new file file_put_contents($cd."/FormInput_php.xml", $newXml); } if(isset($_POST['search'])) { //query the document $name = $_POST['studentname']; $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; $count=0; $size=count($array); echo "<center>"; echo '<form id="contactform" name="contactform" method="post">'; while($count!=count($array)){ foreach ($array[$count]->children() as $child) { $getElementTag=$child->getName(); echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>'; echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "</form>"; echo "***************************"; echo "</center>"; } ?> номер <?php $xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object"); if (isset($_POST['modify'])) { $oldfirstname=($_POST['oldfirstname']); $firstname=($_POST['firstname']); $lastname=($_POST['lastname']); $student_id=($_POST['student_id']); $gender=($_POST['gender']); $dob=($_POST['dob']); $email=($_POST['email']); $mobilenumber=($_POST['mobilenumber']); $address=($_POST['address']); $cohort=($_POST['cohort']); $programme=($_POST['programme']); $mode=($_POST['mode']); $xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="student[firstname=\''.$oldfirstname.'\']"> <xsl:copy> <firstname>'.$firstname.'</firstname> <lasttname>'.$lastname.'</lasttname> <student_id>'.$student_id.'</student_id> <gender>'.$gender.'</gender> <dob>'.$dob.'</dob> <email>'.$email.'</email> <mobilenumber>'.$mobilenumber.'</mobilenumber> <address>'.$address.'</address> <cohort>'.$cohort.'</cohort> <programme>'.$programme.'</programme> <mode>'.$mode.'</mode> </xsl:copy> </xsl:template> </xsl:transform>'; $xsl = new DOMDocument; $xsl->loadXML($xslstr); // Configure the transformer $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); // Transform XML source $newXml = $proc->transformToXML($xml); // Save into new file file_put_contents($cd."/FormInput_php.xml", $newXml); } if(isset($_POST['search'])) { //query the document $name = $_POST['studentname']; $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; $count=0; $size=count($array); echo "<center>"; echo '<form id="contactform" name="contactform" method="post">'; while($count!=count($array)){ foreach ($array[$count]->children() as $child) { $getElementTag=$child->getName(); echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>'; echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "</form>"; echo "***************************"; echo "</center>"; } ?> номер <?php $xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object"); if (isset($_POST['modify'])) { $oldfirstname=($_POST['oldfirstname']); $firstname=($_POST['firstname']); $lastname=($_POST['lastname']); $student_id=($_POST['student_id']); $gender=($_POST['gender']); $dob=($_POST['dob']); $email=($_POST['email']); $mobilenumber=($_POST['mobilenumber']); $address=($_POST['address']); $cohort=($_POST['cohort']); $programme=($_POST['programme']); $mode=($_POST['mode']); $xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="student[firstname=\''.$oldfirstname.'\']"> <xsl:copy> <firstname>'.$firstname.'</firstname> <lasttname>'.$lastname.'</lasttname> <student_id>'.$student_id.'</student_id> <gender>'.$gender.'</gender> <dob>'.$dob.'</dob> <email>'.$email.'</email> <mobilenumber>'.$mobilenumber.'</mobilenumber> <address>'.$address.'</address> <cohort>'.$cohort.'</cohort> <programme>'.$programme.'</programme> <mode>'.$mode.'</mode> </xsl:copy> </xsl:template> </xsl:transform>'; $xsl = new DOMDocument; $xsl->loadXML($xslstr); // Configure the transformer $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); // Transform XML source $newXml = $proc->transformToXML($xml); // Save into new file file_put_contents($cd."/FormInput_php.xml", $newXml); } if(isset($_POST['search'])) { //query the document $name = $_POST['studentname']; $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; $count=0; $size=count($array); echo "<center>"; echo '<form id="contactform" name="contactform" method="post">'; while($count!=count($array)){ foreach ($array[$count]->children() as $child) { $getElementTag=$child->getName(); echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>'; echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "</form>"; echo "***************************"; echo "</center>"; } ?> - <?php $xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object"); if (isset($_POST['modify'])) { $oldfirstname=($_POST['oldfirstname']); $firstname=($_POST['firstname']); $lastname=($_POST['lastname']); $student_id=($_POST['student_id']); $gender=($_POST['gender']); $dob=($_POST['dob']); $email=($_POST['email']); $mobilenumber=($_POST['mobilenumber']); $address=($_POST['address']); $cohort=($_POST['cohort']); $programme=($_POST['programme']); $mode=($_POST['mode']); $xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="student[firstname=\''.$oldfirstname.'\']"> <xsl:copy> <firstname>'.$firstname.'</firstname> <lasttname>'.$lastname.'</lasttname> <student_id>'.$student_id.'</student_id> <gender>'.$gender.'</gender> <dob>'.$dob.'</dob> <email>'.$email.'</email> <mobilenumber>'.$mobilenumber.'</mobilenumber> <address>'.$address.'</address> <cohort>'.$cohort.'</cohort> <programme>'.$programme.'</programme> <mode>'.$mode.'</mode> </xsl:copy> </xsl:template> </xsl:transform>'; $xsl = new DOMDocument; $xsl->loadXML($xslstr); // Configure the transformer $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); // Transform XML source $newXml = $proc->transformToXML($xml); // Save into new file file_put_contents($cd."/FormInput_php.xml", $newXml); } if(isset($_POST['search'])) { //query the document $name = $_POST['studentname']; $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; $count=0; $size=count($array); echo "<center>"; echo '<form id="contactform" name="contactform" method="post">'; while($count!=count($array)){ foreach ($array[$count]->children() as $child) { $getElementTag=$child->getName(); echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>'; echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "</form>"; echo "***************************"; echo "</center>"; } ?> в <?php $xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object"); if (isset($_POST['modify'])) { $oldfirstname=($_POST['oldfirstname']); $firstname=($_POST['firstname']); $lastname=($_POST['lastname']); $student_id=($_POST['student_id']); $gender=($_POST['gender']); $dob=($_POST['dob']); $email=($_POST['email']); $mobilenumber=($_POST['mobilenumber']); $address=($_POST['address']); $cohort=($_POST['cohort']); $programme=($_POST['programme']); $mode=($_POST['mode']); $xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="student[firstname=\''.$oldfirstname.'\']"> <xsl:copy> <firstname>'.$firstname.'</firstname> <lasttname>'.$lastname.'</lasttname> <student_id>'.$student_id.'</student_id> <gender>'.$gender.'</gender> <dob>'.$dob.'</dob> <email>'.$email.'</email> <mobilenumber>'.$mobilenumber.'</mobilenumber> <address>'.$address.'</address> <cohort>'.$cohort.'</cohort> <programme>'.$programme.'</programme> <mode>'.$mode.'</mode> </xsl:copy> </xsl:template> </xsl:transform>'; $xsl = new DOMDocument; $xsl->loadXML($xslstr); // Configure the transformer $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); // Transform XML source $newXml = $proc->transformToXML($xml); // Save into new file file_put_contents($cd."/FormInput_php.xml", $newXml); } if(isset($_POST['search'])) { //query the document $name = $_POST['studentname']; $query = $xml->xpath("/students/student[firstname = '$name']"); $array=$query; $count=0; $size=count($array); echo "<center>"; echo '<form id="contactform" name="contactform" method="post">'; while($count!=count($array)){ foreach ($array[$count]->children() as $child) { $getElementTag=$child->getName(); echo '<label>'.$getElementTag.'</label>'." "; echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>'; echo "<br>"; echo "<br>"; } $count++; } echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>'; echo '<input type="submit" name="modify" value="Update Record">'.'<br>'; echo "</form>"; echo "***************************"; echo "</center>"; } ?> 

Вход в HTML

Вход в форму HTML

Выход XML

(новый файл не перезаписывает существующие, см., как Jess StackOverflow заменяет старый Джон Сноу)

 <?xml version="1.0" encoding="UTF-8"?> <students> <student> <firstname>Jess</firstname> <lasttname>Stackoverflow</lasttname> <student_id>999999</student_id> <gender>female</gender> <dob>10-05-16</dob> <email>JStackoverflow@example.com</email> <mobilenumber>7777777</mobilenumber> <address>Example, Place</address> <cohort>HGJD13D</cohort> <programme>Web development</programme> <mode>FT</mode> </student> <student> <firstname>Jey</firstname> <lastname>Lacroix</lastname> <student_id>150501</student_id> <gender>M</gender> <dob>1990-02-22</dob> <email>Jey@hotmail.com</email> <mobilenumber>57553536</mobilenumber> <address>Curepipe</address> <cohort>BSE15AFT</cohort> <programme>software engineering</programme> <mode>FT</mode> </student> </students> номер <?xml version="1.0" encoding="UTF-8"?> <students> <student> <firstname>Jess</firstname> <lasttname>Stackoverflow</lasttname> <student_id>999999</student_id> <gender>female</gender> <dob>10-05-16</dob> <email>JStackoverflow@example.com</email> <mobilenumber>7777777</mobilenumber> <address>Example, Place</address> <cohort>HGJD13D</cohort> <programme>Web development</programme> <mode>FT</mode> </student> <student> <firstname>Jey</firstname> <lastname>Lacroix</lastname> <student_id>150501</student_id> <gender>M</gender> <dob>1990-02-22</dob> <email>Jey@hotmail.com</email> <mobilenumber>57553536</mobilenumber> <address>Curepipe</address> <cohort>BSE15AFT</cohort> <programme>software engineering</programme> <mode>FT</mode> </student> </students> номер <?xml version="1.0" encoding="UTF-8"?> <students> <student> <firstname>Jess</firstname> <lasttname>Stackoverflow</lasttname> <student_id>999999</student_id> <gender>female</gender> <dob>10-05-16</dob> <email>JStackoverflow@example.com</email> <mobilenumber>7777777</mobilenumber> <address>Example, Place</address> <cohort>HGJD13D</cohort> <programme>Web development</programme> <mode>FT</mode> </student> <student> <firstname>Jey</firstname> <lastname>Lacroix</lastname> <student_id>150501</student_id> <gender>M</gender> <dob>1990-02-22</dob> <email>Jey@hotmail.com</email> <mobilenumber>57553536</mobilenumber> <address>Curepipe</address> <cohort>BSE15AFT</cohort> <programme>software engineering</programme> <mode>FT</mode> </student> </students> номер <?xml version="1.0" encoding="UTF-8"?> <students> <student> <firstname>Jess</firstname> <lasttname>Stackoverflow</lasttname> <student_id>999999</student_id> <gender>female</gender> <dob>10-05-16</dob> <email>JStackoverflow@example.com</email> <mobilenumber>7777777</mobilenumber> <address>Example, Place</address> <cohort>HGJD13D</cohort> <programme>Web development</programme> <mode>FT</mode> </student> <student> <firstname>Jey</firstname> <lastname>Lacroix</lastname> <student_id>150501</student_id> <gender>M</gender> <dob>1990-02-22</dob> <email>Jey@hotmail.com</email> <mobilenumber>57553536</mobilenumber> <address>Curepipe</address> <cohort>BSE15AFT</cohort> <programme>software engineering</programme> <mode>FT</mode> </student> </students> номер <?xml version="1.0" encoding="UTF-8"?> <students> <student> <firstname>Jess</firstname> <lasttname>Stackoverflow</lasttname> <student_id>999999</student_id> <gender>female</gender> <dob>10-05-16</dob> <email>JStackoverflow@example.com</email> <mobilenumber>7777777</mobilenumber> <address>Example, Place</address> <cohort>HGJD13D</cohort> <programme>Web development</programme> <mode>FT</mode> </student> <student> <firstname>Jey</firstname> <lastname>Lacroix</lastname> <student_id>150501</student_id> <gender>M</gender> <dob>1990-02-22</dob> <email>Jey@hotmail.com</email> <mobilenumber>57553536</mobilenumber> <address>Curepipe</address> <cohort>BSE15AFT</cohort> <programme>software engineering</programme> <mode>FT</mode> </student> </students>