XSLT - Chỉnh sửa XML
Dữ liệu được lưu trữ trong các tệp XML có thể được chỉnh sửa từ trình duyệt Internet.
Mở, chỉnh sửa và lưu XML
Bây giờ, chúng tôi sẽ hướng dẫn cách mở, chỉnh sửa và lưu tệp XML được lưu trữ trên máy chủ.
Chúng tôi sẽ sử dụng XSL để chuyển đổi tài liệu XML thành dạng HTML. Giá trị của các phần tử XML sẽ được ghi vào các trường đầu vào HTML ở dạng HTML. Biểu mẫu HTML có thể chỉnh sửa được. Sau khi chỉnh sửa dữ liệu, dữ liệu sẽ được gửi trở lại máy chủ và tệp XML sẽ được cập nhật (chúng tôi sẽ hiển thị mã cho cả PHP và ASP).
Tệp XML và tệp XSL
Trước tiên, hãy xem tài liệu 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>
Sau đó, hãy xem biểu định kiểu sau ("tool.xsl"):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="https://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>
Tệp XSL ở trên lặp qua các phần tử trong tệp XML và tạo một trường đầu vào cho mỗi phần tử "trường" XML. Giá trị của thuộc tính "id" của phần tử "trường" XML được thêm vào cả thuộc tính "id" và "name" của mỗi trường đầu vào HTML. Giá trị của mỗi phần tử "giá trị" XML được thêm vào thuộc tính "giá trị" của từng trường đầu vào HTML. Kết quả là một biểu mẫu HTML có thể chỉnh sửa chứa các giá trị từ tệp XML.
Sau đó, chúng ta có biểu định kiểu thứ hai: "tool_updated.xsl". Đây là tệp XSL sẽ được sử dụng để hiển thị dữ liệu XML được cập nhật. Biểu định kiểu này sẽ không tạo ra biểu mẫu HTML có thể chỉnh sửa mà là bảng HTML tĩnh:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="https://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>
Tệp PHP
Trong tệp "tool.xsl" ở trên, hãy thay đổi thuộc tính hành động của biểu mẫu HTML thành "edittool.php".
Trang "edittool.php" chứa hai hàm: Hàm LoadFile() tải và chuyển đổi tệp XML để hiển thị và hàm updateFile() áp dụng các thay đổi cho tệp 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");
}
?>
Mẹo: Nếu bạn không biết cách viết PHP, vui lòng nghiên cứu hướng dẫn PHP của chúng tôi.
Lưu ý: Chúng tôi đang thực hiện chuyển đổi và áp dụng các thay đổi cho tệp XML trên máy chủ. Đây là một giải pháp đa trình duyệt. Máy khách sẽ chỉ lấy lại HTML từ máy chủ - nó sẽ hoạt động trong mọi trình duyệt.
Tệp ASP
Biểu mẫu HTML trong tệp "tool.xsl" ở trên có thuộc tính hành động có giá trị là "edittool.asp".
Trang "edittool.asp" chứa hai hàm: Hàm LoadFile() tải và chuyển đổi tệp XML để hiển thị và hàm updateFile() áp dụng các thay đổi cho tệp 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
%>