ล่าสุดการพัฒนาเว็บบทเรียน
 

XSLT - การแก้ไข XML


ข้อมูลที่เก็บไว้ในไฟล์ XML สามารถแก้ไขได้จากเบราเซอร์อินเทอร์เน็ต


เปิดแก้ไขและบันทึก XML

ตอนนี้เราจะแสดงวิธีการเปิดแก้ไขและบันทึกไฟล์ XML ที่ถูกเก็บไว้บนเซิร์ฟเวอร์

เราจะใช้ XSL ในการแปลงเอกสาร XML ในรูปแบบ HTML ไป ค่าขององค์ประกอบ XML จะถูกเขียนไปยังช่องใส่ HTML ในรูปแบบ HTML ไป รูปแบบ HTML คือสามารถแก้ไขได้ หลังจากแก้ไขข้อมูลข้อมูลที่จะถูกส่งกลับไปยังเซิร์ฟเวอร์และไฟล์ XML จะมีการปรับปรุง (we will show code for both PHP and ASP)


ไฟล์ XML และไฟล์ XSL

ขั้นแรกให้ดูที่เอกสาร 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>

ดูไฟล์ XML

จากนั้นจะดูที่แผ่นลักษณะต่อไปนี้ ("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>

ดูไฟล์ XSL

แฟ้ม XSL ข้างต้น loops ผ่านองค์ประกอบในไฟล์ XML และสร้างช่องใส่หนึ่งสำหรับแต่ละ XML "field" องค์ประกอบ ค่าของ XML โดย "field" องค์ประกอบ "id" แอตทริบิวต์จะถูกเพิ่มทั้ง "id" และ "name" คุณลักษณะของแต่ละช่องใส่ HTML ค่าของแต่ละของ XML "value" องค์ประกอบที่จะถูกเพิ่มใน "value" แอตทริบิวต์ของแต่ละช่องใส่ HTML ผลที่ได้คือรูปแบบ HTML ที่สามารถแก้ไขได้ที่มีค่าจากไฟล์ XML

จากนั้นเราจะมีแผ่นลักษณะที่สอง: "tool_updated.xsl" นี่คือแฟ้ม XSL ที่จะใช้ในการแสดงข้อมูล XML ปรับปรุง แผ่นลักษณะนี้จะไม่ส่งผลในรูปแบบ HTML ที่สามารถแก้ไขได้ แต่ตาราง HTML แบบคงที่:

<?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>

ดูไฟล์ XSL


ไฟล์ ASP

รูปแบบ HTML ใน "tool.xsl" ไฟล์ดังกล่าวข้างต้นมีแอตทริบิวต์การกระทำที่มีค่าของ "edittool.asp"

"edittool.asp" หน้ามีสองฟังก์ชั่นการ loadFile() โหลดฟังก์ชั่นและการแปลงไฟล์ XML สำหรับการแสดงผลและ updateFile() ฟังก์ชั่นใช้การเปลี่ยนแปลงไปยังไฟล์ 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: หากคุณไม่ทราบวิธีการเขียน ASP โปรดศึกษาของเรา กวดวิชา ASP


PHP การไฟล์

ใน "tool.xsl" แฟ้มข้างเปลี่ยนการดำเนินการรูปแบบ HTML ของแอตทริบิวต์ "edittool.php"

"edittool.php" หน้ามีสองฟังก์ชั่นการ loadFile() โหลดฟังก์ชั่นและการแปลงไฟล์ XML สำหรับการแสดงผลและ updateFile() ฟังก์ชั่นใช้การเปลี่ยนแปลงไปยังไฟล์ 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");
}
?>

เคล็ดลับ: หากคุณไม่ทราบวิธีการเขียน PHP, โปรดศึกษาของเรา PHP กวดวิชา

Note: เราจะทำการเปลี่ยนแปลงและการประยุกต์ใช้การเปลี่ยนแปลงไปยังไฟล์ XML บนเซิร์ฟเวอร์ นี้เป็นทางออกที่เบราว์เซอร์ ลูกค้าจะได้รับ HTML กลับมาจากเซิร์ฟเวอร์ - ซึ่งจะทำงานในเบราว์เซอร์