Solved

xml to xml using xslt and php

Posted on 2014-03-31
4
548 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Semi-transparent PNG 5 137
xml attribute DTD attribute differences 3 65
WordPress/WooCommerce security best practices? 2 82
Getting attribute Values using xslt 4 41
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now