Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

replaceChild in LibXML

by rgaddi (Novice)
on Jul 28, 2009 at 17:51 UTC ( [id://783994]=perlquestion: print w/replies, xml ) Need Help??

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

Oh Wise Monks --

I've tried it 10 different ways, and I still can't manage to replace a node in an XML::LibXML::Document. The document ($results) is the output of an XML::LibXSLT stylesheet transformation, and all I want to do is turn any text inside of <hti:wrap></hti:wrap> tags into a text node. Unfortunately, my replaceChild calls are simply ignored; the old nodes remain and the new ones vanish.

Using replaceNode, insertBefore, or insertAfter instead gives me "Use of uninitialized value in subroutine entry". addChild does put the new text nodes into the tree, but appendChild is ignored entirely.

my $results = $stylesheet->transform($source); # Now grab any elements in our namespace my @nodelist = $results->getElementsByTagNameNS($XMLNS, "*"); foreach my $node (@nodelist) { my $newnode; switch ($node->localname()) { case('wrap') { # Pass this all through the Text::Wrap engine. my $sourcetext = $node->textContent(); my $tab = $node->getAttribute('tab') || "\t"; my $first_tab = $node->getAttribute('firsttab') || $tab; my $wrapped_text = wrap($first_tab, $tab, $sourcetext); $newnode = XML::LibXML::Text->new($wrapped_text); } else { die "Unknown processing directive $XMLNS:" . $node->localn +ame() . "\n"; } } if (defined $newnode) { my $parent = $node->parentNode(); $parent->replaceChild($newnode, $node); } }

Any help would be greatly appreciated. I'm running this under Windows XP; not quite sure what problems libxml may have for me this way.

Thanks,
Rob

Replies are listed 'Best First'.
Re: replaceChild in LibXML
by ikegami (Patriarch) on Jul 28, 2009 at 17:58 UTC

    Please give us something we can run.

    And you'd do well to avoid the very problematic Switch module. Use 5.10 if you insist on such a construct. 5.10 introduced a switch statement. See perlsyn.

Re: replaceChild in LibXML
by ramrod (Curate) on Jul 28, 2009 at 18:25 UTC
    getElementsByTagNameNS gets element nodes (as opposed to text nodes). Try using findnodes, for example:
    $results->findnodes("//wrap/text()");
    This way you will replace a text node with another text node.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://783994]
Approved by ELISHEVA
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found