| 1 | # ----------------------------------------------------------------------------- | ||||||||||
| 2 | |||||||||||
| 3 | =encoding utf8 | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Quiq::Xml::LibXml - Funktionale Erweiterungen von XML::LibXML | ||||||||||
| 8 | |||||||||||
| 9 | =head1 BASE CLASS | ||||||||||
| 10 | |||||||||||
| 11 | XML::LibXML | ||||||||||
| 12 | |||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||
| 14 | |||||||||||
| 15 | Dieses Modul lädt XML::LibXML und erweitert dessen Klassen | ||||||||||
| 16 | um zusätzliche Funktionalität. | ||||||||||
| 17 | |||||||||||
| 18 | =cut | ||||||||||
| 19 | |||||||||||
| 20 | # ----------------------------------------------------------------------------- | ||||||||||
| 21 | |||||||||||
| 22 | package Quiq::Xml::LibXml; | ||||||||||
| 23 | 1 | ▶ | use base qw/XML::LibXML/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 1 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 1 | our $VERSION = '1.240'; | |||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | use Encode (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | # ----------------------------------------------------------------------------- | ||||||||||
| 34 | |||||||||||
| 35 | =head1 METHODS | ||||||||||
| 36 | |||||||||||
| 37 | =head2 Erweiterung XML::LibXML::Document | ||||||||||
| 38 | |||||||||||
| 39 | =head3 lookup() - Finde Knoten | ||||||||||
| 40 | |||||||||||
| 41 | =head4 Synopsis | ||||||||||
| 42 | |||||||||||
| 43 | $node = $doc->lookup($xpath); | ||||||||||
| 44 | |||||||||||
| 45 | =head4 Returns | ||||||||||
| 46 | |||||||||||
| 47 | Knoten | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | # ----------------------------------------------------------------------------- | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | *XML::LibXML::Document::lookup = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 54 | 0 | my ($self,$xpath) = @_; | |||||||||
| 55 | |||||||||||
| 56 | 0 | my @nodes = $self->findnodes($xpath); | |||||||||
| 57 | 0 | ▶ | if (!@nodes) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | $self->throw; | |||||||||
| 59 | } | ||||||||||
| 60 | 0 | ▶ | if (@nodes > 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | $self->throw; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | return $nodes[0]; | |||||||||
| 65 | 1 | }; | |||||||||
| 66 | |||||||||||
| 67 | # ----------------------------------------------------------------------------- | ||||||||||
| 68 | |||||||||||
| 69 | =head3 toFormattedString() - Formatiertes XML | ||||||||||
| 70 | |||||||||||
| 71 | =head4 Synopsis | ||||||||||
| 72 | |||||||||||
| 73 | $str = $doc->toFormattedString; | ||||||||||
| 74 | |||||||||||
| 75 | =head4 Returns | ||||||||||
| 76 | |||||||||||
| 77 | Formatiertes XML (UTF-8 encoded String) | ||||||||||
| 78 | |||||||||||
| 79 | =head4 Description | ||||||||||
| 80 | |||||||||||
| 81 | Liefere das XML des Dokumentes in formatierter Darstellung. Im | ||||||||||
| 82 | Unterschied zur Methode $doc->toString() | ||||||||||
| 83 | |||||||||||
| 84 | =over 2 | ||||||||||
| 85 | |||||||||||
| 86 | =item * | ||||||||||
| 87 | |||||||||||
| 88 | werden zunächst alle leeren Textknoten aus dem Dokument entfernt | ||||||||||
| 89 | (da bei "mixed content" die Methode toString() keine | ||||||||||
| 90 | Formatierung vornimmt) | ||||||||||
| 91 | |||||||||||
| 92 | =item * | ||||||||||
| 93 | |||||||||||
| 94 | wird der String UTF-8 encoded geliefert (was die Methode | ||||||||||
| 95 | toString() des Dokuments - im Gegensatz zu anderen Knoten | ||||||||||
| 96 | - nicht tut) | ||||||||||
| 97 | |||||||||||
| 98 | =back | ||||||||||
| 99 | |||||||||||
| 100 | =cut | ||||||||||
| 101 | |||||||||||
| 102 | # ----------------------------------------------------------------------------- | ||||||||||
| 103 | |||||||||||
| 104 | 3 | ▶ | *XML::LibXML::Document::toFormattedString = sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 105 | 3 | my $self = shift; | |||||||||
| 106 | |||||||||||
| 107 | # Entferne alle leeren Text-Knoten | ||||||||||
| 108 | |||||||||||
| 109 | 3 | for my $nod ($self->findnodes('//text()')) { | |||||||||
| 110 | 3 | ▶ | if ($nod !~ /\S/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 1 | $nod->unbindNode; | |||||||||
| 112 | } | ||||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 3 | return Encode::decode('utf-8',$self->toString(1)); | |||||||||
| 116 | 1 | }; | |||||||||
| 117 | |||||||||||
| 118 | # ----------------------------------------------------------------------------- | ||||||||||
| 119 | |||||||||||
| 120 | =head2 Erweiterung XML::LibXML::Node | ||||||||||
| 121 | |||||||||||
| 122 | =head3 removeNode() - Entferne Knoten | ||||||||||
| 123 | |||||||||||
| 124 | =head4 Synopsis | ||||||||||
| 125 | |||||||||||
| 126 | $nod->removeNode; | ||||||||||
| 127 | |||||||||||
| 128 | =head4 Returns | ||||||||||
| 129 | |||||||||||
| 130 | nichts | ||||||||||
| 131 | |||||||||||
| 132 | =head4 Description | ||||||||||
| 133 | |||||||||||
| 134 | Entferne den Knoten aus dem Dokument. | ||||||||||
| 135 | |||||||||||
| 136 | Dies ist ein Alias für die Methode $nod->unbindNode(), deren Name | ||||||||||
| 137 | ein wenig inkonsequent ist in Bezug auf $nod->removeChild(), | ||||||||||
| 138 | $nod->removeChildNodes(). | ||||||||||
| 139 | |||||||||||
| 140 | =cut | ||||||||||
| 141 | |||||||||||
| 142 | # ----------------------------------------------------------------------------- | ||||||||||
| 143 | |||||||||||
| 144 | 1 | *XML::LibXML::Node::removeNode = *XML::LibXML::Node::unbindNode; | |||||||||
| 145 | |||||||||||
| 146 | # ----------------------------------------------------------------------------- | ||||||||||
| 147 | |||||||||||
| 148 | =head1 VERSION | ||||||||||
| 149 | |||||||||||
| 150 | 1.240 | ||||||||||
| 151 | |||||||||||
| 152 | =head1 AUTHOR | ||||||||||
| 153 | |||||||||||
| 154 | Frank Seitz, L<http://fseitz.de/> | ||||||||||
| 155 | |||||||||||
| 156 | =head1 COPYRIGHT | ||||||||||
| 157 | |||||||||||
| 158 | Copyright (C) 2026 Frank Seitz | ||||||||||
| 159 | |||||||||||
| 160 | =head1 LICENSE | ||||||||||
| 161 | |||||||||||
| 162 | This code is free software; you can redistribute it and/or modify | ||||||||||
| 163 | it under the same terms as Perl itself. | ||||||||||
| 164 | |||||||||||
| 165 | =cut | ||||||||||
| 166 | |||||||||||
| 167 | # ----------------------------------------------------------------------------- | ||||||||||
| 168 | |||||||||||
| 169 | 1 | 1; | |||||||||
| 170 | |||||||||||
| 171 | # eof | ||||||||||