[英]Approximating an MVC using XSL, PHP, Perl, XML.
问题:我有一个存储在数据库中的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! →"/></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。
我的问题是:
<html><body><form>
部分并将其放入PHP。 这样看起来会更清洁。 谢谢。
这是一个使用实际框架的示例。 我使用的框架是Perl的Mojolicious ,当然其他人也可以处理这个问题。 我设置了几种数据库处理方法(您可以从原始脚本中进行调整)。 然后我使用内置的XML解析器来更新记录的属性。
最后我设置了两条路线。 如果您访问/
运行,则按要求获取您的页面。 请注意,XML已被转义(在模板中),因此它将文本呈现给浏览器。 如果您喜欢其他形式的显示,可以更改此设置。 如果您访问/record?id=1
您将直接获得XML结果。 这对RESTful接口更有用。
让我们说你把它命名为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! →"/></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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.