Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting xml feed into an html form

Status
Not open for further replies.

lukemack

Technical User
Nov 22, 2005
3
GB
Hi,

I need to transform the following XML feed into an HTML form using php:


(view source to see the XML or it is in the same directory - feed.xml)

at the moment, the script just parses the feed and displays it but I need to somehow get the form elements and options in the XML to appear on the page as an HTML form. Can someone give me a few pointers on how to do that?

many thanks,

lukemack.

current script:

PHP:
<?php

/*
Script to test parsing an xml document

*/

//declare some variables for storing the data from each element

$xmlSource="feed.xml";// the xml file we want to parse

 // function: startElement
// Deals with the starting element
function startElement( $parser, $tagName, $attrs ) {
 echo "<" . strtolower( $tagName ) .">";
}

 // function: endElement
// Deals with the ending element
function endElement( $parser, $tagName ) {
echo "</" . strtolower( $tagName ) ."><br>";
}

// function: charElement
// Deals with the text in between tags
function charElement( $parser, $text ) {
echo "$text";
}

 // Create an xml parser
$xmlParser = xml_parser_create();

// Set up element handler
xml_set_element_handler( $xmlParser, "startElement", "endElement" );

// Set up character handler
xml_set_character_data_handler( $xmlParser, "charElement" );

// Open connection to RSS XML file for parsing.
$fp = fopen($xmlSource, "r" )
or die( "Cannot read RSS data file." );

// Parse XML data from RSS file.
while( $data = fread( $fp, 4096 ) ) {
xml_parse( $xmlParser, $data, feof( $fp ) );
}

// Close file open handler
fclose( $fp );

// Free xml parser from memory
xml_parser_free( $xmlParser );
?>
 
Try building the html form with a xslt stylesheet.

If you go to the zend website you can find tutorials on using xslt and xml with php.

If you are just creating the form then you can just use xslt to create a web page without any server side scripting.

For example, I use the following to fill a option list on a form:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- define the stylesheet -->
<xsl:stylesheet version="1.0" xmlns:mb="[URL unfurl="true"]http://www.mountainbike.host.sk"[/URL] xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] xmlns:fo="[URL unfurl="true"]http://www.w3.org/1999/XSL/Format">[/URL]
	<!-- Set the output to html -->
	<xsl:output method="html"/>
	<!-- Provide a country code parameter -->
	<xsl:param name="countryCode"/>
	<!-- Match the country code element -->
	<xsl:template match="mb:CountryCodes">
		<xsl:for-each select="mb:CountryCode">
			<xsl:sort order="ascending" select="mb:ShortName"/>
			<!-- If the country code is selected set the value of countrySelected to true, otherwise set it to false -->
			<xsl:if test="mb:ShortCode=$countryCode">
				<xsl:variable name="$countrySelected" select="yes"/>
			</xsl:if>
			<xsl:if test="not(mb:ShortCode=$countryCode)">
				<xsl:variable name="$countrySelected" select="no"/>
			</xsl:if>
			<xsl:element name="option">
				<xsl:attribute name="value"><xsl:value-of select="mb:ShortCode"/></xsl:attribute>
				<xsl:if test="mb:ShortCode=$countryCode">
					<xsl:attribute name="selected">selected</xsl:attribute>
				</xsl:if>
				<xsl:for-each select="mb:ShortName">
					<xsl:value-of select="mb:Name"/>
				</xsl:for-each>
			</xsl:element>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

Now, I use PHP to write the output of the stylesheet into the middle of a form and pass a parameter to the stylesheet before it is processed.

Code:
<!-- Read the country codes and names from an xml file -->
            <?php
              //Start the select control
              echo "<select name='Country' mutiple='mutiple' style='width:98%;'>";
              //Add the first option
              echo "<option selected='selected' value=''>All</option>";
              //Add the country codes
                    
              // switch the zend.ze1_compatability_mode to off
              ini_set("zend.ze1_compatibility_mode","0");

              // load the xml file and stylesheet as domdocuments
              $listxsl = new DomDocument();
              $listxsl->load("xmldata/CountryCodeOptionList.xsl");
              $listDom = new DomDocument();
              $listDom->load("xmldata/countryCodes.xml");

              // create the processor and import the stylesheet
              $listProc = new XsltProcessor();
              $listxsl = $listProc->importStylesheet($listxsl);
                    
              // set the parameters for the country code
              if (array_key_exists("Country",$_REQUEST))
                {$listProc->setParameter("","countryCode",$_REQUEST['Country']);}

              // transform and output the xml document
              $outputDom = $listProc->transformToDoc($listDom);
              print $outputDom->saveXML();                    

              //Close the select tag
              echo "</select>"
              
            ?>

This method above allows me to preset the contents of the list box on the form to the country name passed in the url as an argument (e.g.
If you don't need this functionality I would just create the web page using xslt.

Hope this is helpful.
 
this is very helpful - thanks. From my reading today, I realised I probably needed an XSLT stylesheet. A few questions:

Are you calling a class in your code which contains the XML / XSL parsing functions? Are you also using PHP5?

If I am using PHP 4.x and not 5, I think I need to include a separate XSL extension in the PHP build. Is that correct and can you recommend one?

So, I need to create an XSLT stylesheet. DO I then replace the XML parser I'm already using with an XSL parser?

Thanks loads for your help,

lukemack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top