使用 PHP 和 XSL 限制来自 HTML 表单的 XML 数据
Restrict XML data from an HTML form using PHP and XSL
我使用 XSL 从 XML 数据创建了一个 table,其中包含所有奥斯卡提名者(保存各种信息):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Oscars">
<html>
<head>
<style>
th { text-align: left; }
</style>
<title>Oscars</title>
</head>
<body>
<table width="65%">
<tr><th>Year</th><th>Category</th><th>Nominee</th><th>Info</th><th>Won</th></tr>
<xsl:for-each select="Nomination">
<tr>
<td><xsl:value-of select="Year"/></td>
<td><xsl:value-of select="Category"/></td>
<td><xsl:value-of select="Nominee"/></td>
<td><xsl:value-of select="Info"/></td>
<td><xsl:value-of select="Won"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
输出是一个有数千行的 table。我创建了一个非常基本的 HTML 表单,用户可以过滤此 table,并使用 PHP,我只需要显示符合条件的行。
这是我目前在 PHP 文件中所做的,不确定 POST 是否是正确的选项,但我认为这没有什么不同。我不知道如何在这里建立联系。我是 PHP 的超级新手,我真的很难掌握这一点。
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("oscars.xml");
$xslDoc = new DOMDocument();
$xslDoc->load("oscars.xsl");
$proc = new XSLTProcessor();
$xslDoc = $proc->importStylesheet($xslDoc);
$htmlDoc = $proc->transformToDoc($xmlDoc);
print $htmlDoc->saveXML();
?>
<html lang="eng">
<head><title>Oscars</title></head>
<body>
<h1>
<?php
$year = $_POST['year'];
$category = $_POST['$category'];
$nominee = $_POST['nominee'];
$info = $_POST['info'];
if ($year == '2010') {
_// show all rows from table that match year
}
?>
</h1>
</body>
</html>
动态生成您的 xsl 转换
PHP擅长生成xml,XSL擅长过滤转换
您可以使用选择器 Nomination[year='2010']
将您的输入 xml 过滤到仅 <Nomination>
个节点,其中 year
的值为 2010。PHP 可以动态生成这些选择器。
例如:
<xml version="1.0" encoding="UTF-8">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Oscars">
<html>
<head>
<style>
th { text-align: left; }
</style>
<title>Oscars</title>
</head>
<body>
<table width="65%">
<tr><th>Year</th><th>Category</th><th>Nominee</th><th>Info</th><th>Won</th></tr>
<xsl:for-each select="Nomination<?php
$year = 2010;
if ($year) {
print "[year='$year']";
}
?>">
<tr>
<td><xsl:value-of select="Year"/></td>
<td><xsl:value-of select="Category"/></td>
<td><xsl:value-of select="Nominee"/></td>
<td><xsl:value-of select="Info"/></td>
<td><xsl:value-of select="Won"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
将类似的东西放入函数或其自己的文件中,然后使用 post 中的适当变量调用它,然后在 [=] 中使用 that 14=]打电话。
我会使用这种方法:
使用 xsl:param 制作 xslt 并使用 php.
提供它们
php:
<?php
$xmlUri = '/some/path/to/your.xml';
$xslUri = '/some/path/to/your.xsl';
$xmlDocument = new DOMDocument;
$xslDocument = new DOMDocument;
if ($xmlDocument->load($xmlUri) && $xslDocument->load($xslUri)) {
$year = $_POST['year'];
$category = $_POST['category'];
$nominee = $_POST['nominee'];
$info = $_POST['info'];
$xsltProc = new XSLTProcessor();
$xsltProc->setParam('year', $year);
$xsltProc->setParam('category', $category);
$xsltProc->setParam('nominee', $nominee);
$xsltProc->setParam('info', $info);
if ($xsltProc->importStyleSheet($xslDocument)) {
echo $xsltProc->transformToXML($xmlDocument);
}
}
你的 xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="year"/>
<xsl:param name="category"/>
<xsl:param name="nominee"/>
<xsl:param name="info"/>
<xsl:template match="/Oscars">
<html>
<head>
<style>
th { text-align: left; }
</style>
<title>Oscars</title>
</head>
<body>
<table width="65%">
<tr><th>Year</th><th>Category</th><th>Nominee</th><th>Info</th><th>Won</th></tr>
<xsl:for-each select="Nomination[
Year [.=$year or string-length($year)=0]
and Category[.=$category or string-length($category)=0]
and Nominee [.=$nominee or string-length($nominee)=0]
and Info [.=$info or string-length($info)=0]
]">
<tr>
<td><xsl:value-of select="Year"/></td>
<td><xsl:value-of select="Category"/></td>
<td><xsl:value-of select="Nominee"/></td>
<td><xsl:value-of select="Info"/></td>
<td><xsl:value-of select="Won"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我使用 XSL 从 XML 数据创建了一个 table,其中包含所有奥斯卡提名者(保存各种信息):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Oscars">
<html>
<head>
<style>
th { text-align: left; }
</style>
<title>Oscars</title>
</head>
<body>
<table width="65%">
<tr><th>Year</th><th>Category</th><th>Nominee</th><th>Info</th><th>Won</th></tr>
<xsl:for-each select="Nomination">
<tr>
<td><xsl:value-of select="Year"/></td>
<td><xsl:value-of select="Category"/></td>
<td><xsl:value-of select="Nominee"/></td>
<td><xsl:value-of select="Info"/></td>
<td><xsl:value-of select="Won"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
输出是一个有数千行的 table。我创建了一个非常基本的 HTML 表单,用户可以过滤此 table,并使用 PHP,我只需要显示符合条件的行。
这是我目前在 PHP 文件中所做的,不确定 POST 是否是正确的选项,但我认为这没有什么不同。我不知道如何在这里建立联系。我是 PHP 的超级新手,我真的很难掌握这一点。
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("oscars.xml");
$xslDoc = new DOMDocument();
$xslDoc->load("oscars.xsl");
$proc = new XSLTProcessor();
$xslDoc = $proc->importStylesheet($xslDoc);
$htmlDoc = $proc->transformToDoc($xmlDoc);
print $htmlDoc->saveXML();
?>
<html lang="eng">
<head><title>Oscars</title></head>
<body>
<h1>
<?php
$year = $_POST['year'];
$category = $_POST['$category'];
$nominee = $_POST['nominee'];
$info = $_POST['info'];
if ($year == '2010') {
_// show all rows from table that match year
}
?>
</h1>
</body>
</html>
动态生成您的 xsl 转换
PHP擅长生成xml,XSL擅长过滤转换
您可以使用选择器 Nomination[year='2010']
将您的输入 xml 过滤到仅 <Nomination>
个节点,其中 year
的值为 2010。PHP 可以动态生成这些选择器。
例如:
<xml version="1.0" encoding="UTF-8">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Oscars">
<html>
<head>
<style>
th { text-align: left; }
</style>
<title>Oscars</title>
</head>
<body>
<table width="65%">
<tr><th>Year</th><th>Category</th><th>Nominee</th><th>Info</th><th>Won</th></tr>
<xsl:for-each select="Nomination<?php
$year = 2010;
if ($year) {
print "[year='$year']";
}
?>">
<tr>
<td><xsl:value-of select="Year"/></td>
<td><xsl:value-of select="Category"/></td>
<td><xsl:value-of select="Nominee"/></td>
<td><xsl:value-of select="Info"/></td>
<td><xsl:value-of select="Won"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
将类似的东西放入函数或其自己的文件中,然后使用 post 中的适当变量调用它,然后在 [=] 中使用 that 14=]打电话。
我会使用这种方法:
使用 xsl:param 制作 xslt 并使用 php.
提供它们php:
<?php
$xmlUri = '/some/path/to/your.xml';
$xslUri = '/some/path/to/your.xsl';
$xmlDocument = new DOMDocument;
$xslDocument = new DOMDocument;
if ($xmlDocument->load($xmlUri) && $xslDocument->load($xslUri)) {
$year = $_POST['year'];
$category = $_POST['category'];
$nominee = $_POST['nominee'];
$info = $_POST['info'];
$xsltProc = new XSLTProcessor();
$xsltProc->setParam('year', $year);
$xsltProc->setParam('category', $category);
$xsltProc->setParam('nominee', $nominee);
$xsltProc->setParam('info', $info);
if ($xsltProc->importStyleSheet($xslDocument)) {
echo $xsltProc->transformToXML($xmlDocument);
}
}
你的 xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="year"/>
<xsl:param name="category"/>
<xsl:param name="nominee"/>
<xsl:param name="info"/>
<xsl:template match="/Oscars">
<html>
<head>
<style>
th { text-align: left; }
</style>
<title>Oscars</title>
</head>
<body>
<table width="65%">
<tr><th>Year</th><th>Category</th><th>Nominee</th><th>Info</th><th>Won</th></tr>
<xsl:for-each select="Nomination[
Year [.=$year or string-length($year)=0]
and Category[.=$category or string-length($category)=0]
and Nominee [.=$nominee or string-length($nominee)=0]
and Info [.=$info or string-length($info)=0]
]">
<tr>
<td><xsl:value-of select="Year"/></td>
<td><xsl:value-of select="Category"/></td>
<td><xsl:value-of select="Nominee"/></td>
<td><xsl:value-of select="Info"/></td>
<td><xsl:value-of select="Won"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>