stefzody has asked for the wisdom of the Perl Monks concerning the following question:

Hello there, recently i got asked to branch into XML and all the assorted joys that go along with it (xslt, xpath, etc)

Anyway, I have a parser running and my XML, and i am trying to put options into a select box, using values that I have in a database. I can get the data out and finding the correct select to 'replace' or update with the options.

I have managed to get some code that looks roughly as follows:

my @list = $tree->findnodes('//select[@id="categoryID"]') || warn "cou +ldnt find any nodes for select & name of categoryID"; while (my $row=$cat_sth->fetchrow_hashref ) { my $node = XML::LibXML::Element->new('option'); $node->setAttribute('value',$row->{id}); if ($row->{id} == $params->{categoryID}) { $node->setAttribute('selected','selected'); } my $text = XML::LibXML::Text->new($row->{description}); $node->appendChild($text); $list[0]->push($node);

Now, the obvious question is, what am i doing wrong ? When the page is printed out, none of the other values that i put in present. (Yes, i have also made sure that the values are retrieved from the database, i think this is more a 'misunderstanding' of NodeLists, XML::LibXML and how to add new nodes into it)

thanks

Replies are listed 'Best First'.
Re: LibXML 'Fun'
by dws (Chancellor) on Oct 12, 2003 at 00:23 UTC
    Now, the obvious question is, what am i doing wrong?

    Is that a literal copy/paste of your code? If so, proper (or at least consistent) indentation might help sort things out, or at least would help to rule out potential culprits. Looking at your code, my first impression was a scoping issue, along the lines of

    while ( my $row = ... ) { my $node = ... } $list[0]->push($node); # node may be undef
    Indentation would have made this one easier to rule out. My second impression is that $list[0] isn't really where you want to put your new tree, but then you're not showing us code that prints the page, so I can't tell.

    Please consider a small, complete example that demonstrates the problem. The process of producing such an example is often sufficient to flush out the culprit.

      Hey there, Thanks for the thought, i dont think it is scoping but, in the interest of 'full disclosure' here the code. its a Handler for Apache/mod_perl, however i dont think that this is a problem with that, more LibXML. Anyway, here is the code:
      sub start { my ($r,$params) = @_; # open the database handle and get the data from the table my $db=LMS2::Library->open_lms(); my $cat_sth = $db->prepare("SELECT * FROM CompCat ORDER BY descrip +tion "); $cat_sth->execute; my $parser = XML::LibXML->new(); my $XMLPath = $r->server_root_relative("LMS2/XML"); my $location = $r->location(); my $tree=$parser->parse_file($XMLPath."/Competancy.xml"); my $first; # prepare list of categories my @list2 = $tree->findnodes('//select[@id="categoryID"]') || warn "couldnt find any nodes for select & name of cate +goryID"; while (my $row=$cat_sth->fetchrow_hashref ) { my $node = XML::LibXML::Element->new('option'); $node->setAttribute('value',$row->{id}); if ($row->{id} == $params->{categoryID}) { $node->setAttribute('selected','selected'); } my $text = XML::LibXML::Text->new($row->{description}); $node->appendChild($text); $list2[0]->push($node); } my $parser2 = XML::LibXSLT->new(); my $ss=$parser2->parse_stylesheet_file($XMLPath."/HTMLConvert.xsl" +); my $res=$ss->transform($tree); my $htmlpage = $ss->output_string($res); $htmlpage =~ s/__HOST__/$hn{Host}/gim; $r->print($htmlpage); }
        I'm ignorant of this aspect of LibXML, but are you sure that
        $list2[0]->push($node);
        side-effects $tree?

        Should I assume that you're printing a content-header in code that you haven't shown? Or is the content header part of the XSL?

Re: LibXML 'Fun'
by delirium (Chaplain) on Oct 12, 2003 at 00:57 UTC
    I'm not familiar with this module, so this may be off base, but it looks like you are creating the option tags, but not the select tag that encloses them. Can you show us the HTML that gets printed out?

    Other than that, you might want to try something along the lines of Data::Dumper or run this through the debugger and check the contents of $text, $node, and $list[2] at different stages.