2

PHPを使用してレコードを追加、編集、および削除するためのフォーム/テーブルを作成したいxmlファイルがあります。現在、simpleXML を使用して XML ファイルをロードし、そのコンテンツをさまざまなページに表示しています。

すべての結果を表示するテーブルを作成し、XML ファイル内の完全なレコードを表すテーブルの特定の行を編集または削除できる方法はありますか?

[編集] をクリックすると、レコードの詳細をユーザーが変更して保存できるフォームに表示し、XML ファイルと対応する Web ページを更新したいと考えています。

PHPでこれを行うには、できればSimpleXMLを使用する必要がありますが、PHPでこれを行う他の方法の提案も受け付けています。

乾杯

4

3 に答える 3

3

XSLT は、XML データベース ファイルを Web ページに表示したい形式に変換するための友です。各レコードに必要なすべての HTML を含む XSL テンプレートを作成し、for-each ステートメントを使用して XML ファイルを反復処理します。大まかな概要を説明し、必要に応じて詳細を説明します。

これは、AJAX を介して XSLT (XSL ファイルを使用して XML ファイルを処理する) を行うために使用する一般的な PHP ファイルです。これは、ブラウザーからの GET 呼び出しで渡された必須 (および必要に応じてオプション) の入力で動作するようにセットアップされています。p#n と p#v (以下のコードの上部にあるコメントを参照) は、入力パラメーターを使用して出力に影響を与えたい場合に、XSL ドキュメントに渡されるパラメーターと値のペアです。この場合、出力はブラウザにエコー バックされます。以下は、XML データベースを実行するための PHP と、XSLT 変換を使用して、Web 表示用の HTML (テーブル、または XSL ファイル テンプレートに入力したもの) を作成するものです。

<?php
//REQUIRED INPUTS:
// - xml: path to xml document
// - xsl: path to xsl style sheet
// - pCount: number of parameters to be passed to xslt (send zero '0' if none)
//OPTIONAL INPUTS (must have as many as specified in pCount, increment '1' in
//names below up a number for each iteration):
// - p1n: name of first parameter
// - p1v: value of first parameter

//SET Paths 
$xmlPath = $_GET['xml'];
$xslPath = $_GET['xsl'];

// Load the XML source
$xml = new DOMDocument;
$xml->load($xmlPath);

$xsl = new DOMDocument;
$xsl->load($xslPath);

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

//Set Parameter(s), if present
$xslParamCount = $_GET['pCount']; //Check number of xsl parameters specified in GET
for ($i=1; $i<=$xslParamCount; $i++){
   $xslParamName = $_GET['p'.$i.'n'];
   $xslParamValue = $_GET['p'.$i.'v'];
   $proc->setParameter( '', $xslParamName, $xslParamValue);    //Set parameters for XSLTProcessor
}

// TRANSFORM
echo $proc->transformToXML($xml);

// SET Mime Type
$mime = "application/xhtml+xml";
$charset = "iso-8859-1";
header("Content-Type: $mime;charset=$charset");
?> 

以下は、XML データベース ドキュメントを取得し、それを HTML に変換して Web ページに挿入する XSL ファイル テンプレートの例です。HTML スパン タグに注意してください。すべての xsl タグは、テンプレート内の HTML タグ内およびその周辺にあるものを決定する処理命令です。この場合、結果をフィルタリングし、XSL ファイルに渡された入力パラメーターに基づいて表示する適切なデータを選択しています (上部近くの xsl:param 項目を参照してください)。

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
 <xsl:param name='testId'/>
 <!--Input options for following param: localUse, notLocalUse, nottestId, or '' (all)-->
 <xsl:param name='matchType'/>
 <xsl:template match='/'>
   <xsl:choose>
      <xsl:when test="$matchType='localUse'">
         <xsl:for-each select="//test[@id=$testId and @localUse='yes']">
            <xsl:sort select="../@id"/>
            <div><xsl:if test='../@localPrefTestId=$testId'><xsl:attribute name='class'>preferredTest</xsl:attribute></xsl:if>
               <span class='productStockCode'>
                  <xsl:if test='../@localPrefTestId=$testId'>
                     <xsl:attribute name='title'>Preferred test for this product</xsl:attribute>
                  </xsl:if>
                  <xsl:if test='../@localPrefTestId!=$testId'>
                     <xsl:attribute name='title'>Alternate (not preferred) test for this product - see note to right</xsl:attribute>
                  </xsl:if>
                  <xsl:value-of select='../@id'/>
               </span>
               <span class='productStockName'>
                  <xsl:if test='../@localPrefTestId=$testId'>
                     <xsl:attribute name='title'>Preferred test for this product</xsl:attribute>
                  </xsl:if>
                  <xsl:if test='../@localPrefTestId!=$testId'>
                     <xsl:attribute name='title'>Alternate (not preferred) test for this product - see note to right</xsl:attribute>
                  </xsl:if>
                  <xsl:value-of select='../@name'/>
               </span>
               <span class='producttestNote'>
                  <xsl:value-of select='child::localPrefNote'/>
               </span>
               <span class='productDeleteButton'>
                  <input onClick='remProdLink(this)' title='Click to remove link to this product' type='image' src='button_tiny_X_grey.bmp'></input>
               </span>
            </div>
         </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
         <p>Server Error: GET must specify matchType parameter value of 'localUse', 'notLocalUse', 'nottestId', or '' (for all)</p>
         <p>matchType received: <xsl:value-of select='$matchType'/></p>
      </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
 <!--Note the output method="html" below is required for this to insert correctly into a web page; without this it may nest improperly if any divs are empty-->
 <xsl:output method="html" omit-xml-declaration="yes"/>
</xsl:stylesheet>

すべての xsl テストは XPath 式であることに注意してください。

行を削除するには、「this」引数 (上記のコードの onClick='remProdLink(this)' を参照) を使用して JavaScript 関数を呼び出すボタンをその行に配置して、行を参照し、その行の一意の識別子を取得します。次のような JavaScript の行:

function remProdLink(obj){
   //get unique id via Dom from passed in object reference
   //edit everything after "obj" below to get to the unique id in the record
   var testCode = obj.parentNode.parentNode.firstChild.nextSibling.innerHTML;
   //code to send AJAX POST to server with required information goes here
}

サーバー側では、PHP は一意の識別子を含む AJAX POST を受け取り、XML データベース ファイルを simpleXml にロードし、XPath を介してノードを見つけて削除します。次のようになります。

<?php
   //Move url encoded post data into variables
   $testCode = $_POST['testCode']; //$testCode should be a unique id for the record

   //load xml file to edit
   $xml = simplexml_load_file('yourDatabase.xml');

   //find target node for removal with XPath
   $targets = $xml->xpath("//testCode[@id=$testCode]");

   //import simpleXml reference into Dom to do removal
   $dom2 = dom_import_simplexml($targets[0]);
   $dom2->parentNode->removeChild($dom2);

   //format xml to save indented tree (rather than one line) and save
   $dom = new DOMDocument('1.0');
   $dom->preserveWhiteSpace = false;
   $dom->formatOutput = true;
   $dom->loadXML($xml->asXML());
   $dom->save('yourDatabase.xml');
?>

アイテムの編集に関しては、上記の削除用と同様に別の JavaScript 関数を呼び出し、Web ページのそのアイテムの下に [変更を保存] ボタンを含むフォームを作成し、そのボタンが押されたときに別の JavaScript 関数を呼び出して、 AJAX POST をサーバーに送信します。これも削除と同様です。今回のみ、レコード内で編集可能なすべての情報と、レコードの一意の ID を POST に含める必要があります。PHP ファイルは適切なレコードを検索し (削除の場合と同じ)、PHP でそのレコードの一部を編集するか、単に削除して新しいバージョンのレコードを作成して追加することができます。

必要な詳細の数がわかりません。うまくいけば、これで良いスタートが切れます。詳細が必要な場合は、私の回答にコメントしてください。幸運を!

于 2009-11-25T08:23:09.787 に答える
1

SimpleXml ではなく、DomDocument と DomXPath を使用することをお勧めします。ただし、一般に、XML はデータの保存に最適な媒体ではありません。何をするにもデータベースを使用する必要があります。データを簡単に交換する必要がある場合は、より一般的な MySql の代わりに SQLite を使用できます。

于 2008-12-18T12:07:29.920 に答える
0

ネイティブ XML データベースを使用して、xml ドキュメントおよびノー​​ドの作成、追加、更新、および取得を容易に行うことができます。過去にBerkeleyDBXML(現在はOracleの一部)を使用して成功しました。PHP ライブラリも利用できます。

于 2009-01-05T05:46:26.980 に答える