The instance data you'll use in this tip is shown in Listing 1.
Listing 1. The instance document
<auctionItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<auctionItem itemID="2">
<purchaseDate>2006-09-18</purchaseDate>
<auctionLength>600</auctionLength>
<purchasePrice>10</purchasePrice>
<description>Vintage hair dryer</description>
<estimatedValue>N/A</estimatedValue>
</auctionItem>
<auctionItem itemID="5">
<purchaseDate>2006-09-18</purchaseDate>
<auctionLength>300</auctionLength>
<purchasePrice>4</purchasePrice>
<description>Box Lot</description>
<estimatedValue>N/A</estimatedValue>
</auctionItem>
</auctionItems>
|
Next you'll take a look at the form needed to call your Perl script.
Sending a request to a Perl script
Making the POST request to a Perl script is similar to how you do it using HTML. Take a look at the form in Listing 2.
Listing 2. Submitting a request to a Perl script
<?xml version="1.0" encoding="ASCII"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xforms="http://www.w3.org/2002/xforms">
<head>
<title>Instance Data-To-XHTML/XForms Example</title>
<xforms:model id="auctionItems">
<xforms:instance src="auctions.xml"/>
<xforms:submission id="submit_model_auctionItems"
action=http://localhost/cgi-bin/xformsPerl.pl
method="post"/>
</xforms:model>
<link href="gen_default.css" rel="stylesheet"/>
</head>
<body>
<h1 align="center">Auction Item Inventory</h1>
<xforms:repeat nodeset="auctionItem" id="repeatAuctionItems">
<h2 align="center">Auction Item</h2>
<xforms:input ref="@itemID">
<xforms:label>Item ID</xforms:label>
</xforms:input>
<xforms:input ref="purchaseDate">
<xforms:label>Purchase Date</xforms:label>
</xforms:input>
<xforms:input ref="auctionLength">
<xforms:label>Auction Length</xforms:label>
</xforms:input>
<xforms:input ref="purchasePrice">
<xforms:label>Purchase Price</xforms:label>
</xforms:input>
<xforms:input ref="description">
<xforms:label>Description</xforms:label>
</xforms:input>
<xforms:input ref="estimatedValue">
<xforms:label>Estimated Value</xforms:label>
</xforms:input>
</xforms:repeat>
<xforms:group>
<xforms:trigger>
<xforms:label>Insert Auction Item</xforms:label>
<xforms:insert nodeset="auctionItem"
at="index('repeatAuctionItems')"
position="after" ev:event="DOMActivate" />
</xforms:trigger>
<xforms:trigger>
<xforms:label>Delete Auction Item</xforms:label>
<xforms:delete nodeset="auctionItem"
at="index('repeatAuctionItems')"
ev:event="DOMActivate" />
</xforms:trigger>
</xforms:group>
<xforms:submit submission="submit_model_auctionItems">
<xforms:label>Submit</xforms:label>
</xforms:submit>
</body>
</html>
|
Note the line in bold. The submit button is linked to that submission tag, and to make it submit to your Perl script, simply set the URL of the action attribute to the URL of your Perl script, which in this case is http://localhost/cgi-bin/xformsPerl.pl. Next you'll use Perl to process the request.
Processing the request using Perl
Listing 3 contains the Perl script that receives the XForms POST request.
Listing 3. Capturing and displaying
POST requests using Perl
#!perl
use warnings;
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
print "Content-type: text/xml\n\n";
my $data = param('POSTDATA');
print $data;
|
The first two use statements tell Perl what warning reporting and syntanx checking you want. The second two tell Perl that you're using a CGI style script. Next, the header is displayed with the type of the contents being text/xml. The POSTed data is then retrieved from the param('POSTDATA') variable. The last print statement displays the XML to the browser, as shown in Figure 1.
Figure 1. Displaying the XForms XML instance data using Perl
Great! Now you can work with the XML however you want using Perl's XML::Simple or any other Perl XML module. See the Resources for an IBM developerWorks resource to help you get started using Perl and XML.
You're now able to create an XForm that submits POST requests to your Perl scripts, and then capture and work with the XML instance data that was sent from the form. If you're using a Web programming language as popular as Perl, then adding XForms to the mix is going to be an exciting new adventure for you, and you're already well on your way.
| Description | Name | Size | Download method |
|---|---|---|---|
| Perl XForms samples | xforms_perl_source.zip | 4KB | HTTP |
Information about download methods
- Participate in the discussion forum.
-
For an article that will help you get started working with Perl in XML, see
XML and scripting languages (developerWorks, February 2000).
-
To learn more about other possible schema types for your XForms inputs, check out w3cschools.
-
This series is a good guide for getting started using XForms.
-
Start here to learn about the Visual XForms Designer from alphaWorks.
-
In the
XML area on developerWorks, get the resources
you need to advance your skills in XForms and other XML technologies.
-
Browse the
technology bookstore for books on these and other technical topics.





