Link to home
Start Free TrialLog in
Avatar of shart68
shart68

asked on

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...
Avatar of Jeff Darling
Jeff Darling
Flag of United States of America image

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

Avatar of shart68
shart68

ASKER

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???
ASKER CERTIFIED SOLUTION
Avatar of Jeff Darling
Jeff Darling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shart68

ASKER

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