gpt4 book ai didi

php - 使用 XSL、PHP、Perl、XML 近似 MVC。

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:45:08 25 4
gpt4 key购买 nike

问题:我有一个存储在数据库中的 XML 片段,我想将它与数据库中的其他几个字段结合起来,并使用 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. 有没有办法删除 <html><body><form>部分在 XSL 中并将其放入 PHP 中。这样看起来会干净得多。

谢谢。

最佳答案

这是一个使用实际框架的示例。我使用的框架是Mojolicious对于 Perl,当然其他人也可以处理这个问题。我设置了几种数据库处理方法(您可以从原始脚本中改编)。然后我使用内置的 XML 解析器来更新记录的属性。

最后我设置了两条路线。运行时,如果您访问 /,您将获得请求的页面。请注意,XML 已被转义(在模板中),因此它可以作为文本呈现给浏览器。如果您喜欢其他形式的显示,可以更改此设置。如果您访问 /record?id=1,您将直接获得 XML 结果。这对于 RESTful 接口(interface)更有用。

假设您将其命名为 app.pl。要运行它,您可以简单地

./app.pl daemon

然后访问 http://localhost:3000 以使用 mojo 的内置服务器之一进行查看(是的,它也在 CGI 下运行)。

或者您实际上可以与脚本进行交互。假设您只想在命令行中查看一条记录

./app.pl get '/record?id=1'

或者您可能想插入一些东西,helper 可以通过 eval

获得
./app.pl eval 'app->insert(2, "Something", "<Record>Something Else</Record>", "Name")'

很酷吧?

#!/usr/bin/env perl

use Mojolicious::Lite;
use Mojo::DOM;
use DBI;

# connect to database
use DBI;

helper db => sub {
state $dbh = DBI->connect("dbi:SQLite:database.db","","") or die "Could not connect";
};

# add helper methods for interacting with database
helper create_table => sub {
my $self = shift;
warn "Creating table 'records'\n";
$self->db->do('CREATE TABLE records (id INT, description TEXT, xml_content TEXT, name VARCHAR(255));');
$self->insert(1,'Description','<Record>Contents</Record>','Name');
};

helper select => sub {
my $self = shift;
my $sth = eval { $self->db->prepare('SELECT * FROM records WHERE id = ?') } || return undef;
my $id = shift or return 0;
$sth->execute($id);
return $sth->fetchrow_hashref;
};

helper insert => sub {
my $self = shift;
my ($id, $description, $xml, $name) = @_;
my $sth = eval { $self->db->prepare('INSERT INTO records VALUES (?,?,?,?)') } || return undef;
$sth->execute($id, $description, $xml, $name);
return 1;
};

# if statement didn't prepare, assume its because the table doesn't exist
defined app->select or app->create_table;

helper 'xml_by_id' => sub {
my $self = shift;
my $id = shift;

my $row = $self->select($id) || {};
return '<Record></Record>' unless keys %$row;

my $xml = Mojo::DOM->new->xml(1)->parse( $row->{xml_content} );
my $record = $xml->at('Record');

for my $key ( qw/ name id description / ) {
$record->{$key} = $row->{$key};
}

return wantarray ? ($xml, $row) : $xml;
};

any '/record' => sub {
my $self = shift;
my $id = $self->param('id') || $self->render_not_found;
my $xml = $self->xml_by_id($id);
$self->render( text => $xml, format => 'xml' );
};

any '/' => sub {
my $self = shift;

if ( my $id = $self->param('id') ) {
my ($xml, $row) = $self->xml_by_id($id);
$self->stash( id => $id );
$self->stash( xml => $xml );
$self->stash( row => $row );
}

$self->render('index');
};

app->start;

__DATA__

@@ index.html.ep
<!DOCTYPE html>
<html>
<head><title>Get XML</title></head>
<body>
<form action="/" 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>
% if ( my $id = stash('id') ) {
<p> XML Content ID <%= $id %>
% my $row = stash('row');
% if ( keys %$row ) {
Found
<%= $row->{description} %> - <%= $row->{name} %>
%} else {
NOT FOUND
%}
</p>
%= stash('xml')
% }
</body>
</html>

关于php - 使用 XSL、PHP、Perl、XML 近似 MVC。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13819261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com