XSLT Editing XML

In an XML file, data stored can be edited from a browser.

Open, Edit and Save XML:

To open, edit, and save an XML file that is stored on the server, we first need to transform the XML document into an HTML form using the XSL. To the HTML input fields in the editable HTML form, the values of the XML elements will be written. The data will be submitted back to the server and the XML file will be updated after the data is edited.

The XML File and the XSL File:

book.xml:



  
    ABC6
  
  
    2020
  
  
    100.00
  

book.xsl:





  
  
  

Book Info (edit):

Explanation:

In the above example, the XSL file creates one input field for each XML “field” element, after looping through the elements in the XML file. To both the “id” and “name” attributes of each HTML input field, the value of the XML “field” element’s “id” attribute is added, while, to the “value” attribute of each HTML input field, the value of each XML “value” element is added. The resulting HTML form is editable and contains the values from the XML file.

book_updated.xsl:





  
  
  

Updated Book Info:

Explanation:

In the above XSL file, we are displaying the updated XML data. Instead of an editable HTML form, a static HTML table will be the output of this style sheet.

The PHP File:

editbook.php:

load($xml);

$xslDoc = new DOMDocument();
$xslDoc->load($xsl);

$proc = new XSLTProcessor();
$proc->importStyleSheet($xslDoc);
echo $proc->transformToXML($xmlDoc);
}

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;
   }
 }
}

$xmlLoad->asXML($xml);
loadFile($xml,"book_updated.xsl");
}

if($_POST["btn_sub"] == "")
{
 loadFile("book.xml", "book.xsl");
}
else
{
 updateFile("book.xml");
}
?>

Explanation:

We are now changing the HTML form’s action attribute to “editbook.php” in the “book.xsl” file. In the above PHP page, there are two functions. To load and transform the XML file for display, we are using the loadFile() function and to apply the changes to the XML file, we are using the updateFile() function. Thus, to the XML file on the server, the transformation is done and the changes are applied. The HTML will be given back to the client from the server. Being a cross-browser solution, it will work in any browser.

The ASP File:


function loadFile(xmlfile,xslfile)
Dim xmlDoc,xslDoc
'Load XML and XSL file
set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xmlDoc.load(xmlfile)
set xslDoc = Server.CreateObject("Microsoft.XMLDOM")
xslDoc.async = false
xslDoc.load(xslfile)
'Transform file
Response.Write(xmlDoc.transformNode(xslDoc))
end function

function updateFile(xmlfile)
Dim xmlDoc,rootEl,f
Dim i
'Load XML file
set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xmlDoc.load(xmlfile)

'Set the rootEl variable equal to the root element
Set rootEl = xmlDoc.documentElement

'Loop through the form collection
for i = 1 To Request.Form.Count
 'Eliminate button elements in the form
 if instr(1,Request.Form.Key(i),"btn_")=0 then
   'The selectSingleNode method queries the XML file for a single node
   'that matches a query. This query requests the value element that is
   'the child of a field element that has an id attribute which matches
   'the current key value in the Form Collection. When there is a match -
   'set the text property equal to the value of the current field in the
   'Form Collection.
   set f = rootEl.selectSingleNode("field[@id='" & _
   Request.Form.Key(i) & "']/value")
   f.Text = Request.Form(i)
 end if
next

'Save the modified XML file
xmlDoc.save xmlfile

'Release all object references
set xmlDoc=nothing
set rootEl=nothing
set f=nothing

'Load the modified XML file with a style sheet that
'allows the client to see the edited information
loadFile xmlfile,server.MapPath("book_updated.xsl")
end function

'If form is submitted, update the XML file and display result
' - if not, transform the XML file for editing
if Request.Form("btn_sub")="" then
 loadFile server.MapPath("book.xml"),server.MapPath("book.xsl")
else
 updateFile server.MapPath("book.xml")
end if

Explanation:

Here, we are changing the HTML form’s action attribute to “editbook.asp” in the “book.xsl” file. In the above ASP page, there are two functions. To load and transform the XML file for display, we are using the loadFile() function and to apply the changes to the XML file, we are using the updateFile() function.

Please follow and like us:
Content Protection by DMCA.com