Re: XML::XPath createNode with the same name??

Robin Berjon <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
On Apr 7, 2010, at 15:57 , Brian wrote:
>> Normally, it's possible; chances are the bug is on your side. But it's
>> hard for us to help without at least a description of the issues you're
>> seeing (silent failure? errors? hard drive wiped?) and if possible the
>> code you're using to do this.
> 
> I'm absolutely convinced the bug is on my side.  At least I am hopeful it
> is....
> 
> The original code I was using to build the recipient nodes was the
> following (assuming $self->{'recipientList'} is a semi-colon delimited
> list of emails):
> 
>   if (defined($self->{'recipientList'})) {
>       my @recipients = split(/;/,$self->{'recipientList'});
>        foreach my $recipient (@recipients) {
>         my $string =
> "/llbeanDocument/document/notificationRequest/notificationRequestHdr/recipientList/recipient";
>         $notificationRequest->createNode($string);
>         $notificationRequest->setNodeText($string,$recipient);
>       }
>    }

Yeah, that won't work. What createNode() does is that it looks at the path you provide, and creates any steps in the XPath that are needed to make it exist (but as far as I can tell it doesn't do filtering conditions, it's really a simple hack rather than something general-purpose). This means that the second time you call it it won't do anything (because the path matches) and using a [$1] won't work either (since it treats that as part of the element name).

What you probably want is something like:


# assuming $xp holds the XML::XPath object
if (defined($self->{'recipientList'})) {
    my @recipients = split /;/, $self->{'recipientList'};
    my $rls = $xp->findnodes("/llbeanDocument/document/notificationRequest/notificationRequestHdr/recipientList")->get_node(1);
    for my $rec (@recipients) {
        my $r = XML::XPath::Element->new("recipient");
        $r->appendChild(XML::XPath::Text->new($rec));
        $rls->appendChild($r);
    }
}

No promises that this works though, I don't think I've used XML::XPath since the 90s :) But the idea is there: you get the parent element through XPath (if it doesn't exist, that's something that you can use the createNode trick for first), you create a new element and give it some text, and finally append the new element to its intended parent.

-- 
Robin Berjon - http://berjon.com/



_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.