Solved

xml to xml using xslt and php

Posted on 2014-03-31
4
558 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
  • 2
  • 2
4 Comments
 
LVL 12

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 12

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

733 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