최신 웹 개발 튜토리얼
 

XSLT - 편집 XML


XML 파일에 저장된 데이터는 인터넷 브라우저에서 편집 할 수 있습니다.


열기, 편집 및 저장 XML

이제, 우리는 열고 편집하고 서버에 저장되는 XML 파일을 저장하는 방법을 보여줍니다.

우리는 HTML 형태로 XML 문서를 변환하는 XSL을 사용합니다. 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 파일은 상기 XML 파일의 요소들을 반복하고 각 XML에 대한 하나 개의 입력 필드를 만들고 "field" 소자. 는 XML의 값 "field" 요소의 "id" 속성은 모두에 추가됩니다 "id""name" 각 HTML 입력 필드의 속성. 각 XML 값 "value" 요소가 추가된다 "value" 각 HTML 입력 필드의 속성. 결과는 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 파일보기 .


ASP 파일

의 HTML 양식 "tool.xsl" 파일은 위의 값을 갖는 action 속성이있다 "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 양식의 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 자습서를 .

Note: 우리는 변환을하고 서버의 XML 파일에 변경 사항을 적용하고 있습니다. 이것은 크로스 브라우저 솔루션입니다. 모든 브라우저에서 작동합니다 - 클라이언트는 서버에서 다시 HTML을 얻을 것이다.