in reply to Re: XML::Twig not finding an element's parent's text
in thread XML::Twig not finding an element's parent's text
Thanks! That helped.
You can try adding text before the bookmark element to verify Twig prints it out.
That sounded promising but has no effect. Whether before or after the selected element, the text is not available.
You can set the handler expression to text:h[text:bookmark] (i.e. "an h element with a bookmark child") instead and print the h directly instead of the parent.
The thing is that many kinds of elements may contain <text:bookmark text:name="..."/> so the search has to be on text:bookmark as far as I can tell. However, it looks like a slight modification is the way to go: If I select *[text:bookmark] and then go digging deeper from there, that could work:
#!/usr/bin/perl use XML::Twig; use strict; use warnings; my $xml = XML::Twig->new( twig_handlers => { '*[text:bookmark]' => \&handler_bookmark } ); # twig_handlers => { 'text:bookmark' => \&handler_bookmark } ); $xml->parse(\*DATA); print qq(\n-\n); $xml->print; exit(0); sub handler_bookmark { my( $twig, $bookmark)= @_; print qq(OK\n); print $bookmark->text; my @bmk = $bookmark->children('text:bookmark'); foreach my $b (@bmk) { my $anchor = $b->att('text:name'); print "Anchor: ", $anchor, "\n"; } } __DATA__ <?xml version="1.0" encoding="UTF-8"?> <text:h text:style-name="P900" text:outline-level="3"> Bar foo <text:bookmark text:name="_asdfqwerzxcv"/>Foo bar </text:h>
I'll test and get back in a day or so.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: XML::Twig not finding an element's parent's text
by choroba (Cardinal) on May 18, 2025 at 18:44 UTC | |
by mldvx4 (Friar) on May 19, 2025 at 18:44 UTC |