Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2017-02-22 22:15 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (337 votes). Check out past polls.