Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: fix ODT files with line breaks looking poor

by haukex (Archbishop)
on Apr 07, 2019 at 15:40 UTC ( [id://1232245]=note: print w/replies, xml ) Need Help??


in reply to fix ODT files with line breaks looking poor

Using LibreOffice I whipped up the following minimal example, edited down to the minimum needed:

<?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> </office:text></office:body> </office:document-content>

The transform needed of the XML document is something like this: <r><p>a<x>b<s/>c</x>d</p></r>

<r> `-- <p> `+- a +- <x> | `+- b | +- <s> | `- c `- d

To this: <r><p>a<x>b</x></p><p><x>c</x>d</p></r>

<r> `+- <p> | `+- a | `- <x> | `-- b `- <p> `+- <x> | `-- c `- d

Where I think the elements surrounding <s/>, represented here as <x>, could even be nested more than one level, i.e. maybe <r><p>a<x>b<y>c<x>d<s/>e</x>f</y>g</x>h</p></r>

A very interesting problem, unfortunately I don't have enough time to invest at the moment. You definitely shouldn't try to do this with regexes. If the files aren't too big, I'd probably approach this with XML::LibXML...

Update 2: On second thought, a stream-based parser might be easier in this case... hmmm...

Update 1: A skeleton...

use warnings; use strict; use XML::LibXML; my $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> </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:p//text:line-break') or last; die "can't handle <text:line-break> with children: $lb" if $lb->hasChildNodes; my ($p) = $xpc->findnodes('ancestor::text:p[1]',$lb) or die "failed to find <text:p> ancestor of $lb"; print "$p\n"; #DB # ... do something useful here ... $lb->unbindNode; # so this loop terminates } print "#####\n"; print $dom;

Replies are listed 'Best First'.
Re^2: fix ODT files with line breaks looking poor
by choroba (Cardinal) on Apr 07, 2019 at 20:45 UTC
    > If the files aren't too big, I'd probably approach this with XML::LibXML...

    And if they are too big, you can probably still use it, because it provides XML::LibXML::Reader.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^2: fix ODT files with line breaks looking poor
by melutovich (Acolyte) on Apr 07, 2019 at 21:55 UTC
    Took a stab at Option 1; need to test it more but looks promising.
    my $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 &apos; 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;
    Which produces
    <?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>
    

      Here's my solution, it also handles the more deeply nested cases like <r><p>a<x>b<y>c<x>d<s/>e</x>f</y>g</x>h</p></r>:

      use warnings; use strict; use XML::LibXML; my $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> </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'); my $breakat = 'text:line-break'; my $ancestor = 'text:p'; while (1) { my ($br1) = $xpc->findnodes("//$ancestor//${breakat}[1]") or last; die "can't handle <$breakat> with children: $br1" if $br1->hasChildNodes; my ($an1) = $xpc->findnodes("ancestor::${ancestor}[1]",$br1) or die "failed to find <$ancestor> ancestor of $br1"; my $an2 = $an1->cloneNode(1); my ($br2) = $xpc->findnodes(".//${breakat}[1]", $an2) or die "internal error: failed to find <$breakat> in $an2"; for ( my $cur = $br1; $cur!=$an1 ; $cur = $cur->parentNode ) { while ( my $s = $cur->nextSibling ) { $s->unbindNode } } $br1->unbindNode; for ( my $cur = $br2; $cur!=$an2 ; $cur = $cur->parentNode ) { while ( my $s = $cur->previousSibling ) { $s->unbindNode } } $br2->unbindNode; $an1->parentNode->insertAfter($an2, $an1); } print $dom; __END__ <?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:p><text:p te +xt:style-name="P1"><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:p><text:p text +:style-name="P1"><text:span text:style-name="T5"/> </text:p> </office:text></office:body> </office:document-content>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-19 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found