5

問題 : DB に保存されている XML スニペットを、DB の他のいくつかのフィールドと組み合わせて、PHP の HTML を使用して表示したいと考えています。

私の解決策: Perl バックエンド スクリプトがあります。

$query = "select id, description, xml_content, name from table where id = '$id'";

次に、XML を変更してこれらのフィールドを含めます。

$xml_content =~ s|<Record>|<Record name="$name" id="$id" desc="$desc">|i;

次に、XSL ファイルを使用してこれを

  <xsl:output method="html"/>

  <xsl:template match="/">
   <html xmlns="http://www.w3.org/1999/xhtml">
      <body>
        <form action="info.php" method="get" accept-charset="utf-8">
          <label for="id">Display xml for: </label>
          <input type="text" name="id" value="" id="id" size="40"/>
            <p><input type="submit" value="Display it! &#x02192;"/></p>
            </form>
            <xsl:apply-templates/>
      </body>
   </html>
  </xsl:template>

  <xsl:template match="doc:Record">
    <p>
      <xsl:choose>
        <xsl:when test="./@none">
          XML Content ID <xsl:value-of select="@id"/> NOT FOUND 
        </xsl:when>
        <xsl:otherwise>
          XML Content ID <xsl:value-of select="@id"/> Found  
         <xsl:value-of select="@desc"/> - <xsl:value-of select="@name"/> 
      </xsl:otherwise>
      </xsl:choose>
    </p>
  </xsl:template>

次に、PHP を使用して CGI 変数を取得し、perl スクリプトを実行して出力を表示します。

<?php 
if (!empty($_GET['id'])) {
    $command = "getxml.pl --id=" . $_GET['id'];
    $process = proc_open($command, $descriptorspec, $pipes, null, $_SERVER);
    if (is_resource($process)) { 
        $line = stream_get_contents($pipes[1]);
    } else { 
        $line = '<Record none="" desc="' . $command . '"></Record>';
    }
}

header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
echo '<?xml-stylesheet type="text/xsl" href="xmlinfo.xsl"?>';
echo "\n";

if (empty($command)) { 
    #Display the form only.
    $line = '<Record></Record >';
}
echo "$line \n";

?>

PHP は xslt なしで構成されていたため、PHP を使用して HTML で XML を表示するには、これしか考えられませんでした。

私の質問は:

  1. XSLの一部を削除し<html><body><form>て PHP に入れる方法はありますか。その方がずっときれいに見えるでしょう。

ありがとう。

4

1 に答える 1