XSLTFORMS: refresh model from URL?

56 Views Asked by At

I have some dynamic XML - generated in a PHP script. The script takes a single param 'p' - for 'pagenum'. The idea is to show paginated data - allowing the user to select next/previous page of data.

What approach do I take to updating the data with new pages - is it possible that my screen will update with having to reload the page over HTTP?

Here's a snippet of my main page - basically I'm reloading the whole page with a new GET param. (p=1, p=2 etc). (As can be seen, my main page also happens to be PHP - but I'm really doing a whole lot except grabbing the param).

<?php
    header('Content-Type: text/xml; charset=utf-8');
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
$p=1;
if (isset($_GET['p'])) {
    try { $p=(int)$_GET['p']; } catch(Exception $ex) { ; }
}
if (isset($_GET['debug'])) { $debug="yes"; } else { $debug="no"; }
?>
[...]

<xf:model>
    <xf:instance src="data.php?p=<?=$p?>"/>
</xf:model>

</head>
<body>


<xf:repeat ref="videos/video">
    <details>
        <summary>
            <span class="title"><xf:output value="title"/></span>
        </summary>
        <p>
            <xf:output value="description" mediatype="text/html"/>
        </p>
    </details>
</xf:repeat>

EDIT: adding dummy datasource for reference.

<?php
    header('Content-Type: text/xml; charset=utf-8');
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
if (isset($_GET['p'])) { $p=(int)$_GET['p']; } else { $p=1; }

echo "<data xmlns=''>\n";
echo "<videos>\n";

switch ($p) {
    case 1:
        echo "<video><title>Macbeth</title></video><video>Malformed</video>";
        break;
    case 2:
        echo "<video><title>Hamlet</title></video><video>SPACE 2003</video>";
        break;
    case 3:
        echo "<video><title>Romeo And Juliet</title></video><video>Back to the Feature</video>";
    default:
        echo "<video/>";
}

echo "</videos>\n";
echo "</data>\n";
?>
1

There are 1 best solutions below

0
monojohnny On

I got something working - not sure this is the right approach here or not. Posting in case useful for others - and if anybody could advise improvements. I ditched the idea of using URL params on the view itself - instead, altered the model to include 'pagenum' in a 'metadata' section in the instance itself.

XForm Code:

<?xml-stylesheet href="xsltforms/xsltforms.xsl" type="text/xsl"?>
<?xsltforms-options debug="no"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ev="http://www.w3.org/2001/xml-events"
      xmlns:xf="http://www.w3.org/2002/xforms"
      >
<head>

<model xmlns="http://www.w3.org/2002/xforms">
    <instance src="data.php" id="default"/>  

    <submission id="nextpage" method="get" replace="instance" instance="default" serialization="none">
        <resource value="concat('data.php?p=', metadata/pagenum + 1)"/>
        <message ev:event="xforms-submit-error">Cannot load!</message>
    </submission>

    <submission id="prevpage" method="get" replace="instance" instance="default" serialization="none">
        <resource value="concat('data.php?p=', metadata/pagenum - 1)"/>
        <message ev:event="xforms-submit-error">Cannot load!</message>
    </submission>
</model>

</head>
<body>

<p> Page : <xf:output ref="metadata/pagenum"/> </p>
<xf:submit submission="prevpage"><xf:label>prev</xf:label></xf:submit>
<xf:submit submission="nextpage"><xf:label>next</xf:label></xf:submit>

<xf:repeat ref="videos/video">
    <details>
        <summary>
            <xf:output value="title"/>
        </summary>
        <p>
            <xf:output value="description" mediatype="text/html"/>
        </p>
    </details>
</xf:repeat>

</body>
</html>

Data-source:

<?php
    header('Content-Type: text/xml; charset=utf-8');
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
if (isset($_GET['p'])) { $p=(int)$_GET['p']; } else { $p=0; }

function as_xml($title,$description) {
    return sprintf("<video><title>%s</title><description>%s</description></video>",$title,$description);
}

echo "<data xmlns=''>\n";
echo "<metadata><pagenum>".$p."</pagenum></metadata>\n";
echo "<videos>\n";

switch ($p) {
    case 0:
        echo as_xml('Macbeth','The Scottish Play') . "\n";
        echo as_xml('Malformed','<b>Sci Fi Thriller</b>') . "\n";

        break;
    case 1:
        echo as_xml('Hamlet','Something is rotten in the state of Denmmark') . "\n";
        echo as_xml('SPACE 2003','XML <i>in space</i>') . "\n";
        break;
    case 2:
        echo as_xml('Romeo and Juliet','Early Rom Com') . "\n"  ;
        echo as_xml('Back to the Feature','Copyright avoidance.') . "\n";
        break;
    default:
        echo "<video/>";
}

echo "</videos>\n";
echo "</data>\n";
?>

Screenshot:

enter image description here