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の「フィールド」要素は、「ID」と、各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フォームのaction属性の値が「edittool.asp」です。

「Edittool.asp」ページは2つの機能が含まれます。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と任意のブラウザ上で実行することができます。