开发者

PHP to call original XML file when XSL form data submitted

开发者 https://www.devze.com 2023-02-24 20:37 出处:网络
I have a page that uses an XML file matched with an XSL stylesheet to create a form. The XML file is as such:

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-开发者_JAVA技巧>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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消