xml to xml using xslt and php

I have a simple question about editing and (re)saving an xml document online. I noticed the example of how to do so on w3schools.com and it seemed pretty straight forward. However the example ONLY covers xml documents that have the same child tag names

Here's the example xml w3schools uses:

 <tool>
   <field id="prodName">
     <value>HAMMER HG2606</value>
   </field>
   <field id="prodNo">
     <value>32456240</value>
   </field>
   <field id="price">
     <value>$30.00</value>
   </field>
 </tool>

Updating the xml in the example works because each <field> has the same child tag name <value>. (The php function in control of the change:

function updateFile($xml)
 {
$xmlLoad = simplexml_load_file($xml);
$postKeys = array_keys($_POST);

foreach($xmlLoad->children() as $x)
{
  foreach($_POST as $key=>$value)
  {
    if($key == $x->attributes())
    {
      $x->value = $value; (((<---because all of child are called value each entry updates)))
    }
  }
}

the xslt loops through the tags by attribute:

<table border="0">
     <xsl:for-each select="tool/field">
     <tr>
       <td><xsl:value-of select="@id"/></td>
       <td>
       <input type="text">
       <xsl:attribute name="id">
         <xsl:value-of select="@id" />
       </xsl:attribute>
       <xsl:attribute name="name">
         <xsl:value-of select="@id" />
       </xsl:attribute>
       <xsl:attribute name="value">
         <xsl:value-of select="value" />
       </xsl:attribute>
       </input>
       </td>
     </tr>
     </xsl:for-each>
   </table>

my question is how can this work with more than one child tag 2 levels beep?

here's an example of the type of xml structure I mean:
<?xml version="1.0"?>
<EMPLOYMENT_OPPORTUNITIES>
       <OPPORTUNITY id="0">
               <INDEXABLE>0</INDEXABLE>
               <POSITION>Job1</POSITION>
               <DATELINE>03/17/2014</DATELINE>
               <LOCATION>files/examplefile.pdf</LOCATION>
      </OPPORTUNITY>
      <OPPORTUNITY id="1">
               <INDEXABLE>0</INDEXABLE>
               <POSITION>Job2</POSITION>
               <DATELINE>03/17/2014</DATELINE>
                <LOCATION>files/examplefile.pdf</LOCATION>
        </OPPORTUNITY>
</EMPLOYMENT_OPPORTUNITIES>


All of the w3school code in my example can be found here:

http://www.w3schools.com/xsl/xsl_editxml.asp

Thank you...
shart68Asked:
Who is Participating?
 
Jeff DarlingConnect With a Mentor Developer AnalystCommented:
Based on your code, its not really clear what is contained in $_POST



This may give you some ideas on how to use simplexml to update an XML file.

<?php

$xml= simplexml_load_file("./myDataIn.xml"); 

echo "<h2>Before Change</h2>";
echo "<table border='1'>";
foreach ($xml->OPPORTUNITY as $value){

    echo "<tr>";

    echo "<td>";
    echo $value[0]->attributes();
    echo "</td>";
    echo "<td>";
    echo $value->INDEXABLE;
    echo "</td>";
    echo "<td>";
    echo $value->POSITION;
    echo "</td>";
    echo "<td>";
    echo $value->DATELINE;
    echo "</td>";
    echo "<td>";
    echo $value->LOCATION;
    echo "</td>";

    echo "</tr>";
}
echo "</table>";

// Make some change to the data
$xml->OPPORTUNITY->POSITION = "JOB 7";

foreach ($xml->OPPORTUNITY as $value){

if($value->POSITION == "JOB 7"){
  $value->LOCATION = "files/Resume01.pdf";
}
if($value->POSITION == "Job2"){
  $value->LOCATION = "files/Resume02.pdf";
}

}

//Save XML
$xml->asXML("./myDataOut.xml"); 

$xml= simplexml_load_file("./myDataOut.xml"); 

echo "</br>";
echo "<h2>After Change</h2>";

echo "<table border='1'>";
foreach ($xml->OPPORTUNITY as $value){

    echo "<tr>";

    echo "<td>";
    echo $value[0]->attributes();
    echo "</td>";
    echo "<td>";
    echo $value->INDEXABLE;
    echo "</td>";
    echo "<td>";
    echo $value->POSITION;
    echo "</td>";
    echo "<td>";
    echo $value->DATELINE;
    echo "</td>";
    echo "<td>";
    echo $value->LOCATION;
    echo "</td>";

    echo "</tr>";
}
echo "</table>";

?>

Open in new window

0
 
Jeff DarlingDeveloper AnalystCommented:
XML Sample

<?xml version="1.0"?>
<EMPLOYMENT_OPPORTUNITIES>
       <OPPORTUNITY id="0">
               <INDEXABLE>0</INDEXABLE>
               <POSITION>Job1</POSITION>
               <DATELINE>03/17/2014</DATELINE>
               <LOCATION>files/examplefile.pdf</LOCATION>
      </OPPORTUNITY>
      <OPPORTUNITY id="1">
               <INDEXABLE>0</INDEXABLE>
               <POSITION>Job2</POSITION>
               <DATELINE>03/17/2014</DATELINE>
                <LOCATION>files/examplefile.pdf</LOCATION>
        </OPPORTUNITY>
      <OPPORTUNITY id="3">
               <INDEXABLE>1</INDEXABLE>
               <POSITION>Job3</POSITION>
               <DATELINE>03/18/2014</DATELINE>
                <LOCATION>files/examplefile.pdf</LOCATION>
        </OPPORTUNITY>
</EMPLOYMENT_OPPORTUNITIES>

Open in new window


XSL Sample
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>

  <h2>Employee Opportunity Info</h2>
  <table border="1">
    <tr>
     <td>Opportunity</td>
     <td>INDEXABLE</td>
     <td>POSITION</td>
     <td>DATELINE</td>
     <td>LOCATION</td>
     </tr>
     
    <xsl:for-each select="EMPLOYMENT_OPPORTUNITIES/OPPORTUNITY">
    <tr>
      <td><xsl:value-of select="@id"/></td>
      <td><xsl:value-of select="INDEXABLE" /></td>
      <td><xsl:value-of select="POSITION" /></td>
      <td><xsl:value-of select="DATELINE" /></td>
      <td><xsl:value-of select="LOCATION" /></td>
    </tr>
    </xsl:for-each>
  </table>

  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

Open in new window

0
 
shart68Author Commented:
The xsl Sample above does allow the edit.php page to display the xml elements and children, but it doesn't allow you the edit/change the original xml file. I didn't see anything relating to the function:

 function updateFile($xml)
 {
$xmlLoad = simplexml_load_file($xml);
$postKeys = array_keys($_POST);

foreach($xmlLoad->children() as $x)
{
  foreach($_POST as $key=>$value)
  {
    if($key == $x->attributes())
    {
      $x->value = $value; (((<---because all of child are called value each entry updates)))
    }
  }
}

How or what function would you use to edit the elements in the xml document???
0
 
shart68Author Commented:
Hi, first of, I'd like to say thank you & I appreciate how quickly the reply to my previous post came. The array_keys($_POST) refers to the values posted in the xsl form when the user has entered new data to replace what the value was before the edit (if I'm not mistaken).

The code I used came from w3schools.com URL: http://www.w3schools.com/xsl/xsl_editxml.asp 

I used this code because it gives a user (who doesn't know anything about code) the ability to edit xml files and save the changes that were made back to the same file. It is important that the xml file created keep the same name because the webpage that reads the xml sheet (to know which jobs to display) only has one display section that can only read from one xml file.  

I could see the change displayed on screen when I implemented the code you wrote above, but it appears the new data has to be manually entered in and to add it doesn't replace the value set in the actually xml document.

If I had the responsibility of keeping that page updated, I'd be find because I have the editing software ability to do so. Since it has to be user friendly though, I have to have the xml document open, receive edits, and resave automatically through a user interface.

I hope I explained things well enough, thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.