Gli ultimi tutorial di sviluppo web
 

XSLT - Editing XML


I dati memorizzati in file XML possono essere modificati da un browser Internet.


Aprire, modificare e salvare XML

Ora, mostreremo come aprire, modificare e salvare un file XML che viene memorizzato sul server.

Useremo XSL per trasformare il documento XML in un form HTML. I valori degli elementi XML verranno scritti campi di input HTML in un modulo HTML. Il modulo HTML è modificabile. Dopo aver modificato i dati, i dati sta per essere presentata al server e il file XML verrà aggiornato (we will show code for both PHP and ASP) .


Il file XML e il file XSL

In primo luogo, dare un'occhiata al documento XML ("tool.xml") :

<?xml version="1.0" encoding="UTF-8"?>
<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>

Visualizzare il file XML .

Poi, dare un'occhiata al foglio seguente stile ("tool.xsl") :

<?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>
  <form method="post" action="edittool.asp">
  <h2>Tool Information (edit):</h2>
  <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>
  <br />
  <input type="submit" id="btn_sub" name="btn_sub" value="Submit" />
  <input type="reset" id="btn_res" name="btn_res" value="Reset" />
  </form>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

Visualizzare il file XSL .

Il file XSL sopra loop attraverso gli elementi nel file XML e crea un campo di input per ogni XML "field" elemento. Il valore del XML "field" elemento di "id" attributo viene aggiunto sia alla "id" e "name" attributi di ogni campo di input HTML. Il valore di ogni XML "value" elemento viene aggiunto al "value" attributo di ogni campo di input HTML. Il risultato è un modulo HTML modificabile che contiene i valori dal file XML.

Poi, abbiamo un secondo foglio di stile: "tool_updated.xsl" . Questo è il file XSL che verrà utilizzato per visualizzare i dati XML aggiornati. Questo foglio di stile non si tradurrà in un modulo HTML modificabile, ma una tabella HTML statico:

<?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>Updated Tool Information:</h2>
  <table border="1">
    <xsl:for-each select="tool/field">
    <tr>
      <td><xsl:value-of select="@id" /></td>
      <td><xsl:value-of select="value" /></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

Visualizzare il file XSL .


Il file ASP

Il modulo HTML nella "tool.xsl" file di cui sopra ha un attributo azione con un valore di "edittool.asp" .

Il "edittool.asp" pagina contiene due funzioni: I loadFile() carichi di funzioni e trasforma il file XML per la visualizzazione e updateFile() funzione si applica le modifiche al file XML:

<%
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("tool_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("tool.xml"),server.MapPath("tool.xsl")
else
  updateFile server.MapPath("tool.xml")
end if
%>

Tip: Se non sai come scrivere ASP, si prega di studiare la nostra esercitazione ASP .


Il file PHP

Nella "tool.xsl" file di cui sopra, cambiare attributo di azione del form HTML per "edittool.php" .

Il "edittool.php" pagina contiene due funzioni: I loadFile() carichi di funzioni e trasforma il file XML per la visualizzazione e updateFile() funzione si applica le modifiche al file XML:

<?php
function loadFile($xml, $xsl)
{
$xmlDoc = new DOMDocument();
$xmlDoc->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,"tool_updated.xsl");
}

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

Suggerimento: se non si sa come scrivere PHP, si prega di studiare la nostra esercitazione PHP .

Note: Stiamo facendo la trasformazione e applicare le modifiche al file XML sul server. Questa è una soluzione cross-browser. Il cliente potrà solo HTML dal server - che funziona in qualsiasi browser.