当XSL表单数据提交时,PHP调用原始XML文件
我有一个页面,它使用与 XSL 样式表匹配的 XML 文件来创建表单。
XML 文件是这样的:
<?xml version="1.0"?>
<?xml-stylesheet href="XSL/BasicForm.xsl" type="text/xsl"?>
<xml>
<title></title>
<entry1></entry1>
<enrty2></entry2>
<entry3></entry3>
</xml>
它引用来创建表单的 BasicForm.xsl 文件是这样的:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="xml">
<form method="POST" action="action.php">
<xsl:for-each select="child::*">
<label>
<xsl:value-of select="name()"/>:
<input name="{name()}" type="text" />
</label>
<br />
</xsl:for-each>
<br/>
<input type="submit" value="Submit" name="submitButton"/>
<br/>
</form>
</xsl:template>
</xsl:stylesheet>
正如您所看到的,提交表单后,将引用一个 PHP 文件来进行表单处理和打印。 PHP 文件如下所示:
<html>
<head></head>
<body >
<?php
$myFile = ;
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$doc = DOMDocument::loadXML($theData, LIBXML_NOERROR);
if ($doc !== FALSE) {
$text = ''; // used to accumulate output while walking XML tree
foreach ($doc->documentElement->childNodes as $child) {
if ($child->nodeType == XML_TEXT_NODE) { // keep text nodes
$text .= $child->wholeText;
} else if (array_key_exists($child->tagName, $_POST)) {
// replace nodes whose tag matches a POST variable
$text .= $_POST[$child->tagName];
} else { // keep other nodes
$text .= $doc->saveXML($child);
}
}
echo $text . "\n";
} else {
echo "Failed to parse XML\n";
}
?>
</body>
</html>
我需要做的是调用原始 XML 文件并将其与 $myFile 变量相匹配,以便使用表单提交的数据来处理它。我不能只将绝对引用放在变量字段中,因为此过程将使用多个文件(取决于用户单击的文件)。因此,我再次需要找到一种方法,在提交 XSL 表单时调用 PHP 脚本中的原始 XML 文件。
任何帮助都会很棒。
谢谢, 乙
I have a page that uses an XML file matched with an XSL stylesheet to create a form.
The XML file is as such:
<?xml version="1.0"?>
<?xml-stylesheet href="XSL/BasicForm.xsl" type="text/xsl"?>
<xml>
<title></title>
<entry1></entry1>
<enrty2></entry2>
<entry3></entry3>
</xml>
And the BasicForm.xsl file which it references to create the form is as such:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="xml">
<form method="POST" action="action.php">
<xsl:for-each select="child::*">
<label>
<xsl:value-of select="name()"/>:
<input name="{name()}" type="text" />
</label>
<br />
</xsl:for-each>
<br/>
<input type="submit" value="Submit" name="submitButton"/>
<br/>
</form>
</xsl:template>
</xsl:stylesheet>
As you can see, upon submitting the form, a PHP file is referenced for form processing and printing. The PHP file looks like so:
<html>
<head></head>
<body >
<?php
$myFile = ;
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$doc = DOMDocument::loadXML($theData, LIBXML_NOERROR);
if ($doc !== FALSE) {
$text = ''; // used to accumulate output while walking XML tree
foreach ($doc->documentElement->childNodes as $child) {
if ($child->nodeType == XML_TEXT_NODE) { // keep text nodes
$text .= $child->wholeText;
} else if (array_key_exists($child->tagName, $_POST)) {
// replace nodes whose tag matches a POST variable
$text .= $_POST[$child->tagName];
} else { // keep other nodes
$text .= $doc->saveXML($child);
}
}
echo $text . "\n";
} else {
echo "Failed to parse XML\n";
}
?>
</body>
</html>
What I need to do is call upon the original XML file and match it to the $myFile variable in order to process it with the form's submitted data. I can't just put the absolute reference in the variable field because multiple files will be used with this process (depending on what file the user clicks on). So again, I need to find a way to call upon the original XML file within the PHP script when the XSL form is submitted.
Any help would be great.
Thanks,
E
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我并没有完全理解您在哪里“丢失”了原始 XML 文件的内容。您始终可以对 xml 文件的内容进行 url_encode,并将其作为隐藏变量包含在呈现给最终用户的表单中。如果 xml 文件特别大,这可能是不可能的,但在这个只有 4 个值的示例中,您可以将 XML 作为 post 字段传递并在 action.php 中对其进行解码
I'm not exactly following where you're "losing" the contents of the original XML file. You could always url_encode the contents of the xml file and include it as a hidden variable in the form being presented to the end user. This might not be possible if the xml files are particularly large but in this example with only 4 values you could just pass the XML as a post field and decode it in action.php
在转换之前,使用 setParameter() 将文件路径变量传递给 PHP 中的 XSLT 处理命令解决了问题。然后将该参数作为 post 数据调用以调用原始 XML 文件路径。
乙
Problem solved using setParameter() to pass the filepath variable to XSLT process command in PHP just before the transformation. Then the parameter was called as post data to call the original XML filepath.
E