XSLT - XML 편집
XML 파일에 저장된 데이터는 인터넷 브라우저에서 편집할 수 있습니다.
XML 열기, 편집 및 저장
이제 서버에 저장된 XML 파일을 열고, 편집하고, 저장하는 방법을 보여드리겠습니다.
XSL을 사용하여 XML 문서를 HTML 형식으로 변환합니다. XML 요소의 값은 HTML 양식의 HTML 입력 필드에 기록됩니다. HTML 양식은 편집 가능합니다. 데이터를 편집한 후 데이터는 서버로 다시 제출되고 XML 파일이 업데이트됩니다(PHP와 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 파일은 XML 파일의 요소를 반복하고 각 XML "필드" 요소에 대해 하나의 입력 필드를 만듭니다. XML "field" 요소의 "id" 속성 값은 각 HTML 입력 필드의 "id" 및 "name" 속성 모두에 추가됩니다. 각 XML "value" 요소의 값은 각 HTML 입력 필드의 "value" 속성에 추가됩니다. 결과는 XML 파일의 값이 포함된 편집 가능한 HTML 양식입니다.
그런 다음 두 번째 스타일 시트인 "tool_updated.xsl"이 있습니다. 이것은 업데이트된 XML 데이터를 표시하는 데 사용할 XSL 파일입니다. 이 스타일 시트는 편집 가능한 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 파일 을 봅니다.
PHP 파일
위의 "tool.xsl" 파일에서 HTML 폼의 action 속성을 "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 튜토리얼 을 참조하십시오 .
참고: 변환을 수행하고 서버의 XML 파일에 변경 사항을 적용하고 있습니다. 이것은 브라우저 간 솔루션입니다. 클라이언트는 서버에서 HTML만 다시 가져옵니다. 이는 모든 브라우저에서 작동합니다.
ASP 파일
위의 "tool.xsl" 파일에 있는 HTML 양식에는 "edittool.asp" 값을 가진 action 속성이 있습니다.
"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
%>