in reply to Re: fix ODT files with line breaks looking poor
in thread fix ODT files with line breaks looking poor
Which producesmy $dom = XML::LibXML->load_xml(string => <<'END_XML'); <?xml version="1.0"?> <office:document-content office:version="1.2" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"> <office:body><office:text> <text:p text:style-name="P1"> Fo<text:span text:style-name="T1">o<text:line-break/> B</text:span><text:span text:style-name="T3">a</text:span> <text:span text:style-name="T5">r<text:line-break/></text:span> </text:p> <text:p text:style-name="P3"> TEST is a ' real test. </text:p> </office:text></office:body> </office:document-content> END_XML my $xpc = XML::LibXML::XPathContext->new($dom); $xpc->registerNs('office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); $xpc->registerNs('text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'); while (1) { my ($lb) = $xpc->findnodes('//text:line-break') or last; die "can't handle <text:line-break> with children: $lb" if $lb->hasChildNodes; my ($a) = $xpc->findnodes('ancestor::text:*[1]',$lb) or die "failed to find ancestor of $lb"; my ($a_a) = $xpc->findnodes('ancestor::*[1]',$a) or die "failed to find ancestor of ancestor of $a"; my $clone_a = $a->cloneNode(0); my $nextSibling = $lb->nextSibling(); while ( $nextSibling ) { my $currentSibling = $nextSibling; $nextSibling = $currentSibling->nextSibling(); $currentSibling = $a->removeChild($currentSibling); $clone_a->addChild($currentSibling); } $a->removeChild($lb); $a_a->insertAfter($clone_a,$a); } print $dom;
<?xml version="1.0"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.2">
<office:body><office:text>
<text:p text:style-name="P1">
Fo<text:span text:style-name="T1">o</text:span><text:span text:style-name="T1">
B</text:span><text:span text:style-name="T3">a</text:span>
<text:span text:style-name="T5">r</text:span><text:span text:style-name="T5"/>
</text:p>
<text:p text:style-name="P3">
TEST is a ' real test.
</text:p>
</office:text></office:body>
</office:document-content>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: fix ODT files with line breaks looking poor
by haukex (Archbishop) on Apr 08, 2019 at 19:59 UTC |