Latest web development tutorials

XSLT - XML ​​편집기

XML 파일에 저장된 데이터는 인터넷 브라우저를 통해 편집 할 수있다.


열기, 편집 및 XML 저장

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

우리는 HTML 형태로 XSL 문서에 XML을 사용합니다. XML 요소의 값은 HTML HTML 양식 입력 필드에 기록됩니다. 이 HTML 양식 편집 할 수 있습니다. 수정 된 후, 데이터는, XML 파일이 갱신된다 (이 부분은 ASP로 이루어진다)를 다시 서버에 전송한다.


XML 문서와 XSL 파일

우선, ( "tool.xml")에 사용되는 XML 문서를 보면 :

<?xml version="1.0" encoding="ISO-8859-1"?>
<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="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<form method="post" action="edittool.html">
<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 "필드"요소의 입력 필드를 생성한다. 값 "ID"속성 XML의 "필드"요소는 "아이디"및 각 HTML 입력 필드의 "이름"속성에 추가됩니다. 각 XML "값"요소의 값은 각각의 HTML 입력 필드의 "값"속성에 추가됩니다. 결과적으로는 HTML 형식으로 편집 할 수있다 포함하는 XML 파일의 값을 얻을 수있다.

"tool_updated.xsl"다음, 우리는 두 번째 스타일 시트가 있습니다. 이 XSL 파일은 업데이트 된 XML 데이터를 표시하는 데 사용된다. 이 스타일 시트는 출력 편집 가능한 HTML 양식,하지만 정적 HTML 테이블되지 않습니다 :

<?xml version="1.0" encoding="ISO-8859-1"?>
<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 파일

상기 "tool.xsl"파일은 HTML 형식의 액션 속성의 값은 "edittool.asp"이다.

"Edittool.asp"페이지의 두 가지 기능이 포함되어 다 loadFile () 함수로드 및 XML 파일을 변환, updateFile () 함수는 XML 파일을 갱신 :

<%
function loadFile(xmlfile,xslfile)
Dim xmlDoc,xslDoc
'Load XML file
set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xmlDoc.load(xmlfile)
'Load XSL file
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 the form has been 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
%>

팁 : ASP를 작성하는 방법을 모르는 경우, 우리의 공부하시기 바랍니다 ASP 자습서를 .

참고 : 우리는 서버에있는 XML 문서의 변환을 업데이트하고 있습니다.이 크로스 플랫폼 솔루션입니다. 클라이언트는 서버로부터 리턴 된 HTML을 얻을 수있다 -와 HTML 브라우저에서 실행될 수있다.