home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_a687f17fe9680eb192c7c9fe33e407d0
< prev
next >
Wrap
Text File
|
2000-03-23
|
10KB
|
279 lines
<HTML>
<HEAD>
<TITLE>CGI::XMLForm - Extension of CGI.pm which reads/generates formated XML.</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> CGI::XMLForm - Extension of CGI.pm which reads/generates formated XML.</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<UL>
<LI><A HREF="#toxml">toXML</A></LI>
</UL>
<LI><A HREF="#syntax">SYNTAX</A></LI>
<UL>
<LI><A HREF="#readxml">readXML</A></LI>
<LI><A HREF="#caveats">Caveats</A></LI>
</UL>
<LI><A HREF="#author">AUTHOR</A></LI>
<LI><A HREF="#see also">SEE ALSO</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>CGI::XMLForm - Extension of CGI.pm which reads/generates formated XML.</P>
<P>NB: This is a subclass of CGI.pm, so can be used in it's place.</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use CGI::XMLForm;</PRE>
<PRE>
my $cgi = new CGI::XMLForm;</PRE>
<PRE>
if ($cgi->param) {
print $cgi->header, $cgi->pre($cgi->escapeHTML($cgi->toXML));
}
else {
open(FILE, "test.xml") or die "Can't open: $!";
my @queries = ('/a', '/a/b*', '/a/b/c*', /a/d');
print $cgi->header,
$cgi->pre($cgi->escapeHTML(
join "\n", $cgi->readXML(*FILE, @queries)));
}</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>This module can either create form field values from XML based on XQL/XSL style
queries (full XQL is _not_ supported - this module is designed for speed), or it
can create XML from form values. There are 2 key functions: toXML and readXML.</P>
<P>
<H2><A NAME="toxml">toXML</A></H2>
<P>The module takes form fields given in a specialised format,
and outputs them to XML based on that format. The idea is that you
can create forms that define the resulting XML at the back end.</P>
<P>The format for the form elements is:</P>
<PRE>
<input name="/body/p/ul/li"></PRE>
<P>which creates the following XML:</P>
<PRE>
<body>
<p>
<ul>
<li>Entered Value</li>
</ul>
</p>
</body></PRE>
<P>It's the user's responsibility to design appropriate forms to make
use of this module. Details of how come below...</P>
<P>Also supported are attribute form items, that allow creation
of element attributes. The syntax for this is:</P>
<PRE>
<input name="/body/p[@id='mypara' and @onClick='someFunc()']/@class"></PRE>
<P>Which creates the following XML:</P>
<PRE>
<body>
<p id="mypara" onClick="someFunc()" class="Entered Value"></p>
</body></PRE>
<P>Also possible are relative paths. So the following form elements:</P>
<PRE>
<input type="hidden" name="/table/tr">
<input type="text" name="td">
<input type="text" name="td">
<input type="text" name="../tr/td"></PRE>
<P>Will create the following XML:</P>
<PRE>
<table>
<tr>
<td>value1</td>
<td>value2</td>
</tr>
<tr>
<td>value3</td>
</tr>
</table></PRE>
<P>
<HR>
<H1><A NAME="syntax">SYNTAX</A></H1>
<P>The following is a brief syntax guideline</P>
<P>Full paths start with a ``/'' :</P>
<PRE>
"/table/tr/td"</PRE>
<P>Relative paths start with either ``..'' or just a tag name.</P>
<PRE>
"../tr/td"
"td"</PRE>
<P><STRONG>Relative paths go at the level above the previous path, unless the previous
path was also a relative path, in which case it goes at the same level.</STRONG> This
seems confusing at first (you might expect it to always go at the level above
the previous element), but it makes your form easier to design. Take the
following example: You have a timesheet (see the example supplied in the
archive) that has monday,tuesday,etc. Our form can look like this:</P>
<PRE>
<input type="text" name="/timesheet/projects/project/@Name">
<input type="text" name="monday">
<input type="text" name="tuesday">
...</PRE>
<P>Rather than:</P>
<PRE>
<input type="text" name="/timesheet/projects/project/@Name">
<input type="text" name="monday">
<input type="text" name="../tuesday">
<input type="text" name="../wednesday">
...</PRE>
<P>If unsure I recommend using full paths, relative paths are great for repeating
groups of data, but weak for heavily structured data. Picture the following
paths:</P>
<PRE>
/timesheet/employee/name/forename
../surname
title
../department</PRE>
<P>This actually creates the following XML:</P>
<PRE>
<timesheet>
<employee>
<name>
<forename>val1</forname>
<surname>val2</surname>
<title>val3></title>
</name>
<department>val4</department>
</employee>
</timesheet></PRE>
<P>Confusing eh? Far better to say:</P>
<PRE>
/timesheet/employee/name/forename
/timesheet/employee/name/surname
/timesheet/employee/name/title
/timesheet/employee/department</PRE>
<P>Or alternatively, better still:</P>
<PRE>
/timesheet/employee/name (Make hidden and no value)
forename
surname
title
../department</PRE>
<P>Attributes go in square brackets. Attribute names are preceded with an ``@'',
and attribute values follow an ``='' sign and are enclosed in quotes. Multiple
attributes are separated with `` and ''.</P>
<PRE>
/table[@bgcolor="blue" and @width="100%"]/tr/td</PRE>
<P>If setting an attribute, it follows after the tag that it is associated with,
after a ``/'' and it's name is preceded with an ``@''.</P>
<PRE>
/table/@bgcolor</PRE>
<P>
<H2><A NAME="readxml">readXML</A></H2>
<P>readXML takes either a file handle or text as the first parameter and a list of
queries following that. The XML is searched for the queries and it returns a
list of tuples that are the query and the match.</P>
<P>It's easier to demonstrate this with an example. Given the following XML:</P>
<PRE>
<a>Foo
<b>Bar
<c>Fred</c>
<c>Blogs</c>
</b>
<b>Red
<c>Barbara</c>
<c>Cartland</c>
</b>
<d>Food</d>
</a></PRE>
<P>And the following queries:</P>
<PRE>
/a
/a/b*
c*
/a/d</PRE>
<P>it returns the following result as a list:</P>
<PRE>
/a
Foo
/a/b
Bar
c
Fred
c
Blogs
/a/b
Red
c
Barbara
c
Cartland
/a/d
Food</PRE>
<P>(NB: This is slightly incorrect - for /a and /a/b it will return ``Foo\n '' and
``Bar\n '' respectively).</P>
<P>The queries support relative paths like toXML (including parent paths), and
they also support wildcards using ``.*'' or ``.*?'' (preferably ``.*?'' as it's
probably a better match). If a wildcard is specified the results will have the
actual value substituted with the wildcard. Wildcards are a bit experimental,
so be careful ;-)</P>
<P>
<H2><A NAME="caveats">Caveats</A></H2>
<P>There are a few caveats to using this module:</P>
<UL>
<LI><STRONG><A NAME="item_setting">Parameters must be on the form in the order they will appear in the XML.
=item * There is no support for multiple attribute setting (i.e. you can only
set one attribute for an element at a time).
=item * You can't set an attribute <STRONG>and</STRONG> a value for that element, it's one or the
other.
=item * You can use this module in place of CGI.pm, since it's a subclass.
=item * There are bound to be lots of bugs! Although it's in production use
right now - just watch CPAN for regular updates.
=back</A></STRONG><BR>
</UL>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>Matt Sergeant <A HREF="mailto:msergeant@ndirect.co.uk,">msergeant@ndirect.co.uk,</A> <A HREF="mailto:sergeant@geocities.com">sergeant@geocities.com</A></P>
<P>Based on an original concept, and discussions with, Jonathan Eisenzopf.
Thanks to the Perl-XML mailing list for suggesting the XSL syntax.</P>
<P>Special thanks to Francois Belanger (<A HREF="mailto:francois@sitepak.com">francois@sitepak.com</A>) for
his mentoring and help with the syntax design.</P>
<P>
<HR>
<H1><A NAME="see also">SEE ALSO</A></H1>
<P>CGI(1), CGI::XML</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> CGI::XMLForm - Extension of CGI.pm which reads/generates formated XML.</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>