Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

XML::LibXML problem...

by Sinister (Friar)
on Mar 08, 2002 at 13:35 UTC ( #150309=perlquestion: print w/replies, xml ) Need Help??
Sinister has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,
I have a problem regarding XML::LibXML, another great API by Matts.

I have an xml document like this:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE page SYSTEM "entities.dtd"> <form> <method>POST</method> <action>/cgi/process.cgi</action <input> <type>text</type> <name>name1</name> <required>0</required> <value></value> <comment>This field is not required.</comment> </input> <input> <type>text</type> <name>name3</name> <required>1</required> <value></value> <comment>Put three words here!</comment> <test> my @tmp = split(/\ /, shift()); return 1 if ( $#tmp == 2 ); return 0; </test> </input> </form>

The XML above creates a web form through the use of XSLT. The little bit of perl-code is stuffed through ev'l into a closure and the value of the CGI::param("name3") is put into that routine. If 0 returns we show the form again, if 1 returns we continue.

To be nice and informatife, I'd like to tell the user he/she is a stupid user. But I also like to store into my DOM the values to user already submitted. And I want to add another branch to the <form> trunk, holding information on why they failed.

I've tried _a lot_ in adding childs, nodes, childnodes, textchilds, you name it, but I just can't seem to get it done propperly.

I now have a hack, where I do:
$string = $dom->toString; $string =~ s/<\/form>//gim; $string .= "<info><failure>blah</failure></info>\n</form>\n"; $dom = $parser->parse_string($string);
This cannot be the purpose of libXML, and it surely is pretty expensive....

Er formait hyarya.
"Field experience is something you don't get until just after you need it."

Replies are listed 'Best First'.
Re: XML::LibXML problem...
by lestrrat (Deacon) on Mar 08, 2002 at 20:05 UTC

    This seems to work for me:

    use strict; use XML::LibXML; my $xml =<<'EOM'; <?xml version="1.0" encoding="ISO-8859-1" ?> <form> <method>POST</method> <action>/cgi/process.cgi</action> <input> <type>text</type> <name>name1</name> <required>0</required> <value></value> <comment>This field is not required.</comment> </input> <input> <type>text</type> <name>name3</name> <required>1</required> <value></value> <comment>Put three words here!</comment> <test> my @tmp = split(/\ /, shift()); return 1 if ( $#tmp == 2 ); return 0; </test> </input> </form> EOM my $parser = XML::LibXML->new(); my $dom = $parser->parse_string( $xml ); my $info = $dom->createElement( 'info' ); my $failure = $dom->createElement( 'failure' ); my $reason = $dom->createTextNode( 'blah' ); $failure->appendChild( $reason ); $info->appendChild( $failure ); my $form = $dom->getDocumentElement; $form->appendChild( $info ); print $dom->toString;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://150309]
Approved by root
[Lady_Aleena]: 1nickt, I wouldn't, but I'm not normal.
LanX aggrees, LA isn't normal ;-P
[Lady_Aleena]: LanX, thanks bunches.
[Lady_Aleena]: Rigth now I'm ranting in my head about an old subject.
[1nickt]: Hm, that is, how to prevent Perl from turning 1.0 into 1? I feel sure this must be an faq, but am reading perlnum and not finding the answer ...
[Lady_Aleena]: 1nickt, why does it matter in this case?
[1nickt]: Hm, perhaps this? "Operators which expect an integer force the argument into the integer format."
[1nickt]: Can this be? So print
[1nickt]: ... "expects an integer" ?

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2017-05-24 18:33 GMT
Find Nodes?
    Voting Booth?