Solved

xml to xml using xslt and php

Posted on 2014-03-31
4
567 Views
Last Modified: 2014-04-07
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...
0
Comment
Question by:shart68
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 39979207
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
 

Author Comment

by:shart68
ID: 39982955
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
 
LVL 13

Accepted Solution

by:
Jeff Darling earned 500 total points
ID: 39984201
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
 

Author Comment

by:shart68
ID: 39984432
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

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question